Skip to content

BUG: Slicing on Datetimeindex fails #43223

Closed
@benHeid

Description

@benHeid
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions