18
votes

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.

4

4 Answers

2
votes

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)
12
votes

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]
6
votes

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.

1
votes

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.