Skip to content
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

BUG: Index(...).map(na_action="ignore") doesn't work for NaT. #53176

Closed
2 of 3 tasks
joooeey opened this issue May 11, 2023 · 1 comment
Closed
2 of 3 tasks

BUG: Index(...).map(na_action="ignore") doesn't work for NaT. #53176

joooeey opened this issue May 11, 2023 · 1 comment
Labels
Bug Index Related to the Index class or subclasses Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@joooeey
Copy link
Contributor

joooeey commented May 11, 2023

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

i = pd.DatetimeIndex(["2022-01-01", "NaT"])
i.map(lambda x: x.time(), na_action="ignore") # ValueError

Issue Description

The documentation of pd.Index.map describes says:

na_action : {None, 'ignore'}
    If 'ignore', propagate NA values, without passing them to the
    mapping correspondence.

However, NaT is apparently not considered an NA value in this case.

The above code results in the following error

ValueError: NaTType does not support time
Full traceback:
Traceback (most recent call last):

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/extension.py:161 in map
    result = mapper(self)

  Cell In[12], line 1 in <lambda>
    i.map(lambda x: x.time())

TypeError: 'numpy.ndarray' object is not callable


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  Cell In[12], line 1
    i.map(lambda x: x.time())

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/extension.py:171 in map
    return self.astype(object).map(mapper)

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/base.py:6158 in map
    new_values = self._map_values(mapper, na_action=na_action)

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/base.py:924 in _map_values
    new_values = map_f(values, mapper)

  File pandas/_libs/lib.pyx:2834 in pandas._libs.lib.map_infer

  Cell In[12], line 1 in <lambda>
    i.map(lambda x: x.time())

  File pandas/_libs/tslibs/nattype.pyx:59 in pandas._libs.tslibs.nattype._make_error_func.f

ValueError: NaTType does not support time


i.map(lambda x: x.time(), na_action="ignore")
Traceback (most recent call last):

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/extension.py:161 in map
    result = mapper(self)

  Cell In[13], line 1 in <lambda>
    i.map(lambda x: x.time(), na_action="ignore")

TypeError: 'numpy.ndarray' object is not callable


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  Cell In[13], line 1
    i.map(lambda x: x.time(), na_action="ignore")

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/extension.py:171 in map
    return self.astype(object).map(mapper)

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/indexes/base.py:6158 in map
    new_values = self._map_values(mapper, na_action=na_action)

  File ~/mambaforge/envs/moma/lib/python3.11/site-packages/pandas/core/base.py:924 in _map_values
    new_values = map_f(values, mapper)

  File pandas/_libs/lib.pyx:2834 in pandas._libs.lib.map_infer

  Cell In[13], line 1 in <lambda>
    i.map(lambda x: x.time(), na_action="ignore")

  File pandas/_libs/tslibs/nattype.pyx:59 in pandas._libs.tslibs.nattype._make_error_func.f

ValueError: NaTType does not support time

Expected Behavior

If na_action=="ignore", NaT values are propagated or replaced by another suitable NA value, without raising an error.

This is the expected behaviour because:

  • It matches the description of the feature in the documentation.
  • It matches the behaviour of pd.Series.map. (It is interesting to note that the docs of pd.Series.map talk about NaN values as opposed to NA values which appears inaccurate.)

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-135-generic
Version : #152-Ubuntu SMP Wed Nov 23 20:19:22 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.1
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.6.1
pip : 23.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
/home/lukas/mambaforge/envs/moma/lib/python3.11/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

@joooeey joooeey added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 11, 2023
@topper-123
Copy link
Contributor

Thansk for the report, @joooeey. This has been fixed and will work in v2.1.

In [1]: import pandas as pd  # pandas dev version
    ...:
    ...: i = pd.DatetimeIndex(["2022-01-01", "NaT"])
    ...: i.map(lambda x: x.time(), na_action="ignore")
Out[1]: Index([00:00:00, NaT], dtype='object')

Closing.

@topper-123 topper-123 added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 11, 2023
@topper-123 topper-123 added this to the 2.1 milestone May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

2 participants