Skip to content

BUG: Slicing on Datetimeindex fails #43223

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
2 of 3 tasks
benHeid opened this issue Aug 26, 2021 · 3 comments · Fixed by #43428
Closed
2 of 3 tasks

BUG: Slicing on Datetimeindex fails #43223

benHeid opened this issue Aug 26, 2021 · 3 comments · Fixed by #43428
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@benHeid
Copy link
Contributor

benHeid commented Aug 26, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {'col2': [i for i in range(4)]}, 
    index=pd.DatetimeIndex(["11.01.2011 22:00", "11.01.2011 23:00", "12.01.2011 00:00", "2011-01-13 00:00"]))
df["2011-01-01": "2011-11-01"]

Error message:

  File "...\pandas\core\frame.py", line 3438, in __getitem__
    return self._slice(indexer, axis=0)
  File "...\pandas\core\generic.py", line 3826, in _slice
    assert isinstance(slobj, slice), type(slobj)
AssertionError: <class 'numpy.ndarray'>

Problem description

The execution of df["2011-01-01": "2011-11-01"] fails with the following exception: AssertionError: <class 'numpy.ndarray'>. So the first problem is that the error message is not helping to solve the issue. Moreover, I observed that at least in Pandas 1.0.0 the code snippet is executed successfully. Additionally, if the index is sorted the error also does not occur: df.sort_index()["2011-01-01": "2011-11-01"]

Note a similar closed issue exists: #35509. With the current version the code snippet from that issue works. However, mine fails.

Expected Output

                      col2
2011-01-13 00:00:00     3
2011-11-01 22:00:00     0
2011-11-01 23:00:00     1

or helpful error message

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 5f648bf
python : 3.8.6.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 : de_DE.cp1252
pandas : 1.3.2
numpy : 1.18.4
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 53.1.0
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.20.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.18.2
xlrd : None
xlwt : None
numba : None

@benHeid benHeid added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 26, 2021
@simonjayhawkins
Copy link
Member

Thanks @benHeid for the report.

Moreover, I observed that at least in Pandas 1.0.0 the code snippet is executed successfully.

indeed. will label as a regression pending further investigation.

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version Indexing Related to indexing on series/frames, not to indexes themselves Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 26, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Aug 26, 2021
@phofl
Copy link
Member

phofl commented Aug 26, 2021

Just a note: Even if this would work this is deprecated.
You can use loc as an alternative, but is deprecated too

@shubh242
Copy link

shubh242 commented Aug 31, 2021

import pandas as pd
import numpy as np

df = pd.DataFrame(
{'col2': [i for i in range(4)]},
index=pd.DatetimeIndex(["2011-01-11 22:00", "2011-01-11 23:00", "2011-01-12 00:00", "2011-01-13 00:00"]))
df["2011-01-01": "2011-11-01"]

Output:-

Output

Try using same datetime format throughout the list.

@jreback jreback modified the milestones: Contributions Welcome, 1.3.3 Sep 9, 2021
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 Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants