pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.87k stars 18.02k forks source link

BUG:Inconsistent when use Series.get #36248

Open liudengfeng opened 4 years ago

liudengfeng commented 4 years ago

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd
animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama'],index=[-1,0,1,2,3])
animals.get([1,-1,2]) # ok

animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama'])
animals.get([1,2]) # ok

BUT:
animals.get([1,-1,2]) return None!!!

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

Expected Output

If you mix existing labels with non-existent labels, at least need to return the corresponding results of the existing labels?

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d3326dee660824a8433ffd01065f8ac37f7d6 python : 3.8.5.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.18362 machine : AMD64 processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : Chinese (Simplified)_China.936

pandas : 1.1.2 numpy : 1.19.1 pytz : 2020.1 dateutil : 2.8.1 pip : 20.2.2 setuptools : 49.6.0.post20200814 Cython : 0.29.21 pytest : 6.0.1 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.5.2 html5lib : 1.1 pymysql : None psycopg2 : None jinja2 : 2.11.2 IPython : 7.18.1 pandas_datareader: None bs4 : 4.9.1 bottleneck : 1.3.2 fsspec : 0.8.0 fastparquet : None gcsfs : None matplotlib : 3.3.1 numexpr : 2.7.1 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 1.0.1 pytables : None pyxlsb : None s3fs : None scipy : 1.5.2 sqlalchemy : 1.3.19 tables : 3.6.1 tabulate : None xarray : None xlrd : 1.2.0 xlwt : None numba : None

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

phofl commented 4 years ago

Hi,

thanks for your report. This stopped working with release 1.0 as described in https://pandas.pydata.org/docs/user_guide/indexing.html#indexing-deprecate-loc-reindex-listlike. This is more or less translated into animals.loc[[1,-1,2]].

But we could maybe clarify the documentation here?

Edit: Actually that did not work before 1.0 either.

Akashkambale-Creater commented 4 years ago

Hi,

Python read the data line by line, in the second animal data-frame that particular (-1) index is not present that's why it's not returning any value.

You can try

Your code here

import pandas as pd animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama'],index=[-1,0,1,2,3]) print(animals.get([1,-1,2]))

animals = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama']) print(animals.get([1,2]))

print(animals.get([1,2]))