Skip to content

DOC: Improve docstring of Series/DataFrame.bool #34229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,16 +1375,36 @@ def __nonzero__(self):

def bool(self):
"""
Return the bool of a single element PandasObject.
Return the bool of a single element Series or DataFrame.

This must be a boolean scalar value, either True or False. Raise a
ValueError if the PandasObject does not have exactly 1 element, or that
element is not boolean
This must be a boolean scalar value, either True or False. It will raise a
ValueError if the Series or DataFrame does not have exactly 1 element, or that
element is not boolean (integer values 0 and 1 will also raise an exception).

Returns
-------
bool
Same single boolean value converted to bool type.
The value in the Series or DataFrame.

See Also
--------
Series.astype : Change the data type of a Series, including to boolean.
DataFrame.astype : Change the data type of a DataFrame, including to boolean.
numpy.bool : NumPy boolean data type, used by pandaas for boolean values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
numpy.bool : NumPy boolean data type, used by pandaas for boolean values.
numpy.bool : NumPy boolean data type, used by pandas for boolean values.

Two notes: this is not actually the boolean data type (but an alias for built-in bool, the numpy one is np.bool_), and is this reference actually useful? (the method returns a builtin bool, not a numpy one)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, thanks for the feedback.

What I had in mind is that given the name of the method, users may come to this page looking for a way to convert a Series or a DataFrame to boolean (like elementwise). I was trying to add references that may be useful for them. Didn't think of item, but may be could be added here too. Also, not sure if any and all could be useful to some visitors.

Also happy to remove 'numpy.bool_`, it's difficult to draw the line of what could be useful and what is too much.

What would you have?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow reply, fixing it up to refer to np.bool_ instead of np.bool is fine for me


Examples
--------
The method will only work for single element objects with a boolean value:

>>> pd.Series([True]).bool()
True
>>> pd.Series([False]).bool()
False

>>> pd.DataFrame({'col': [True]}).bool()
True
>>> pd.DataFrame({'col': [False]}).bool()
False
"""
v = self.squeeze()
if isinstance(v, (bool, np.bool_)):
Expand Down