Skip to content

replace Pandas item() implementation with numpy's using .values #797

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 2 commits into from
Oct 24, 2019
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
2 changes: 1 addition & 1 deletion pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _scalar_out(input):
else: #
# works if it's a 1 length array and
# will throw a ValueError otherwise
output = input.item()
output = input.values.item()
Copy link
Member

Choose a reason for hiding this comment

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

this won't work if input is a numpy array because it doesn't have a values attribute.

In [3]: np.array([0]).values
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-6306ba001445> in <module>
----> 1 np.array([0]).values

AttributeError: 'numpy.ndarray' object has no attribute 'values'

np.asarray seems to work:

In [9]: np.asarray(pd.Series([0])).item()
Out[9]: 0

In [10]: np.asarray(np.array([0])).item()
Out[10]: 0

Interesting that np.asscalar also emits a warning:

In [7]: np.asscalar(pd.Series([0]))
/Users/holmgren/miniconda3/envs/sfacore/lib/python3.7/site-packages/numpy/lib/type_check.py:547: FutureWarning: `item` has been deprecated and will be removed in a future version
  return a.item()
Out[7]: 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion @wholmgren. I've implemented that solution.

Also, the Numpy docs suggest similarly under np.asscalar.


return output

Expand Down