BUG: Series.get() on ExtensionArray with integer key not in index returns incorrect result · Issue #21257 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd

In [2]: import decimal

In [3]: from pandas.tests.extension.decimal.array import DecimalArray

In [4]: s = pd.Series(DecimalArray([decimal.Decimal(i) for i in range(10)]))

In [5]: s Out[5]: 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 dtype: decimal

In [6]: s2 = s[::2]

In [7]: s2 Out[7]: 0 0 2 2 4 4 6 6 8 8 dtype: decimal

In [8]: s2.get(1) Out[8]: Decimal('2')

In [9]: t = pd.Series([i for i in range(10)], dtype=float)

In [10]: t Out[10]: 0 0.0 1 1.0 2 2.0 3 3.0 4 4.0 5 5.0 6 6.0 7 7.0 8 8.0 9 9.0 dtype: float64

In [11]: t2 = t[::2]

In [12]: t2 Out[12]: 0 0.0 2 2.0 4 4.0 6 6.0 8 8.0 dtype: float64

In [13]: t2.get(1)

In [14]: t2.get(1) is None Out[14]: True

Problem description

It appears that I didn't fix all of the issues in #20885, as I missed an issue when creating #20882 .

If you have an integer index backing an ExtensionArray, and you do .get(n), where n is not in the index, an incorrect result is returned.

Expected Output

In the example above, the value of s2.get(1) should be None, just as it is for t2.get(1).

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None