Skip to content

Indexing with Series.__getitem__ with DatetimeIndex differs based on monotonicity #24892

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

Closed
hsxyl opened this issue Jan 24, 2019 · 2 comments · Fixed by #42265
Closed

Indexing with Series.__getitem__ with DatetimeIndex differs based on monotonicity #24892

hsxyl opened this issue Jan 24, 2019 · 2 comments · Fixed by #42265
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@hsxyl
Copy link

hsxyl commented Jan 24, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd

# generate two Series randomly
t1=pd.Series([1.2,2.2,3.0,4,8],index=pd.date_range(start='2015-5-14 00:00:00',end='2015-5-14 00:04:00',freq='T'))
t2=pd.Series([6.5,7.2,8.1,9],index=pd.date_range(start='2015-5-15 00:00:00',end='2015-5-15 00:03:00',freq='T'))

# merge t2 ,t1  -> t3
t3=pd.concat([t2,t1])

key='2015-5-14 00:00:00'

print('type t1 is ',type(t1))
print('type t3 is ',type(t3))

print('(1):',t1[key])
print('(2):',t3[key])
print('(3):',t3[key:key])

Problem description

type t1 is  <class 'pandas.core.series.Series'>
type t3 is  <class 'pandas.core.series.Series'>
(1): 1.2
(2): 2015-05-14    1.2
dtype: float64
(3): 2015-05-14    1.2
dtype: float64
  • firstly I concat t1 and t2 and got t3 ,than we can compare t1[key] , t3[key] and t3[key:key]
  • I think we expect t3[key] just work like t1[key],but t3[key] work same as t3[key:key]
  • it makes people confused extremely, in fact,I cost hours for that

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 36.4.0
Cython: None
numpy: 1.12.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 7.1.1
sphinx: None
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@mroeschke
Copy link
Member

I agreed that this is pretty inconsistent. Here's a simpler example without the concat step, as overall I think this has to do with whether the index is monotonic:

In [16]: mon_s = pd.Series(range(3), index=pd.date_range(start='2015-5-13 23:59:00', freq='min', periods=3))

In [17]: non_mon_s = pd.Series(range(3), index=[mon_s.index[1], mon_s.index[0], mon_s.index[2]])

In [18]: non_mon_s
Out[18]:
2015-05-14 00:00:00    0
2015-05-13 23:59:00    1
2015-05-14 00:01:00    2
dtype: int64

In [19]: mon_s
Out[19]:
2015-05-13 23:59:00    0
2015-05-14 00:00:00    1
2015-05-14 00:01:00    2
Freq: T, dtype: int64

In [20]: mon_s[key]
Out[20]: 1

In [21]: non_mon_s[key]
Out[21]:
2015-05-14    0
dtype: int64

@mroeschke mroeschke added Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves labels Jan 24, 2019
@mroeschke mroeschke changed the title unexpected behavior after pd.concat Indexing with Series.__getitem__ with DatetimeIndex differs based on monotonicity Jan 24, 2019
@jbrockmendel
Copy link
Member

Using @mroeschke's example, it looks like the behavior diverges when we get to DatetimeIndex._partial_date_slice, which starts with a check:

        is_monotonic = self.is_monotonic
        if (
            is_monotonic
            and reso in ["day", "hour", "minute", "second"]
            and self._resolution >= Resolution.get_reso(reso)
        ):
            # These resolution/monotonicity validations came from GH3931,
            # GH3452 and GH2369.

            # See also GH14826
            raise KeyError

this raises for the monotonic index but not the non-monotonic one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants