Skip to content

DataFrame.drop with empty DatetimeIndex behaves differently than passing empty Index #27994

@katsuya-horiuchi

Description

@katsuya-horiuchi

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({
    {'value': [1, 2]},
    index=[pd.Timestamp('2019-01-01'), pd.Timestamp('2019-01-01')]
})
df = df.drop(df[df['value'] > 10].index)

Problem description

When calling drop for dataframe with DatetimeIndex, the function may raise an exception. See the following traceback:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    df = df.drop(df[df['value'] > 10].index)
  File "pandas/pandas/core/frame.py", line 4042, in drop
    errors=errors,
  File "pandas/pandas/core/generic.py", line 3891, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
  File "pandas/pandas/core/generic.py", line 3940, in _drop_axis
    labels_missing = (axis.get_indexer_for(labels) == -1).any()
  File "pandas/pandas/core/indexes/base.py", line 4769, in get_indexer_for
    indexer, _ = self.get_indexer_non_unique(target, **kwargs)
  File "pandas/pandas/core/indexes/base.py", line 4752, in get_indexer_non_unique
    indexer, missing = self._engine.get_indexer_non_unique(tgt_values)
  File "pandas/_libs/index.pyx", line 291, in pandas._libs.index.IndexEngine.get_indexer_non_unique
    stargets = set(targets)
TypeError: 'NoneType' object is not iterable

This can be avoided by not using DataFrame.drop like this:

 df = df[df['value'] <= 10]

But nonetheless I'm reporting the issue as I expect drop to not raise the exception.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 9f93d57
python : 3.7.4.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.0-5-amd64
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.0.dev0+1184.g9f93d5702
numpy : 1.16.2
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.2
setuptools : 41.1.0
Cython : 0.29.13
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions