Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: `item` has been deprecated

Tags:

python

pandas

So far I used this line of code here:

max_total_gross = event_data["max_total_gross"].loc[event_data["event_id"] == event_id].item()

Since I updated Pandas I receive the future warning:

/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:12: FutureWarning: item has been deprecated and will be removed in a future version if sys.path[0] == '':

I tried to fix it that way, but it's not the same outcome:

event_data.loc[event_data.event_id == event_id, 'max_total_gross']

I expected a single integer.

like image 295
Joey Coder Avatar asked Aug 07 '19 08:08

Joey Coder


People also ask

Why append is deprecated?

append was deprecated because: "Series. append and DataFrame. append [are] making an analogy to list. append, but it's a poor analogy since the behavior isn't (and can't be) in place.

How do I check pandas Jupyter version?

We can use pd. __version__ to check the version of the Pandas running on any system.

How do you use Dtype in pandas?

In order to convert data types in pandas, there are three basic options: Use astype() to force an appropriate dtype. Create a custom function to convert the data. Use pandas functions such as to_numeric() or to_datetime()


Video Answer


4 Answers

You could also just use .iloc[0], but keep in mind that it will raise an IndexError if there is not at least one item in the series you're calling it on.

s = event_data.loc[event_data.event_id == event_id, 'max_total_gross']
s.iloc[0]
like image 65
MoRe Avatar answered Oct 19 '22 18:10

MoRe


The method item() is still useful if you want to assert that the Series has length exactly 1, and also get that single value at the same time. I recommend replacing:

result = ser.item()

with:

result = ser.values.item()

which should do what you want.

like image 36
cxrodgers Avatar answered Oct 19 '22 19:10

cxrodgers


If need first matched value use iter with next, advantage is if no value is matched is returned default value:

s = event_data.loc[event_data.event_id == event_id, 'max_total_gross']

out = next(iter(s), 'no match')
print (out)
like image 45
jezrael Avatar answered Oct 19 '22 20:10

jezrael


It has been undeprecated again in pandas 1.0 for exactly this use case:

Series.item() and Index.item() have been undeprecated (GH29250)

from https://pandas.pydata.org/docs/whatsnew/v1.0.0.html#deprecations. See also discussion in the issue GH29250.

So consider updating or ignoring or silencing the warning instead.

like image 27
Jonas Hörsch Avatar answered Oct 19 '22 19:10

Jonas Hörsch