Skip to content

BUG: AttributeError: 'DatetimeArray' object has no attribute 'notna' in pandas 2.1.1 #55431

Closed
@davidboweninrupt

Description

@davidboweninrupt

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
import datetime

pd.DataFrame( [datetime.datetime.now(datetime.UTC)])[0].value_counts(bins=2)

Issue Description

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib64/python3.11/site-packages/pandas/core/base.py", line 1010, in value_counts
    return algorithms.value_counts_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pandas/core/algorithms.py", line 885, in value_counts_internal
    ii = cut(values, bins, include_lowest=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pandas/core/reshape/tile.py", line 247, in cut
    x, dtype = _coerce_to_type(x)
               ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pandas/core/reshape/tile.py", line 505, in _coerce_to_type
    x = np.where(x.notna(), x.view(np.int64), np.nan)
                 ^^^^^^^
AttributeError: 'DatetimeArray' object has no attribute 'notna'

Note that a potential workaround is to convert the datetime to int and then handle the fallout of that in subsequent code.

pd.DataFrame( [datetime.datetime.now(datetime.UTC)])[0].apply(datetime.datetime.timestamp).value_counts(bins=2)
(1694934883.4420002, 1696631514.958]    1
(1696631514.958, 1698328146.473]        0
Name: count, dtype: int64

Expected Behavior

In pandas 2.1.0 we used to get the binned counts back, such as:

self = 0
(2023-09-17 07:09:20.929712127, 2023-10-06 22:26:32.121833984]    1
(2023-10-06 22:26:32.121833984, 2023-10-26 13:43:43.313955840]    0
Name: count, dtype: int64

Installed Versions

pd.show_versions()
/usr/lib/python3.11/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS
------------------
commit              : e86ed377639948c64c429059127bcf5b359ab6be
python              : 3.11.5.final.0
python-bits         : 64
OS                  : Linux
OS-release          : 6.5.5-200.bz2227221.fc38.x86_64 (custom kernel to fix Logitech dongle issue)
Version             : #1 SMP PREEMPT_DYNAMIC Thu Oct  5 19:57:47 UTC 2023
machine             : x86_64
processor           : 
byteorder           : little
LC_ALL              : None
LANG                : C.UTF-8
LOCALE              : en_US.UTF-8

pandas              : 2.1.1
numpy               : 1.26.0
pytz                : 2023.3.post1
dateutil            : 2.8.2
setuptools          : 65.5.1
pip                 : 22.3.1
Cython              : None
pytest              : 7.4.2
hypothesis          : None
sphinx              : None
blosc               : None
feather             : None
xlsxwriter          : None
lxml.etree          : None
html5lib            : 1.1
pymysql             : None
psycopg2            : None
jinja2              : None
IPython             : None
pandas_datareader   : None
bs4                 : None
bottleneck          : None
dataframe-api-compat: None
fastparquet         : None
fsspec              : None
gcsfs               : None
matplotlib          : None
numba               : None
numexpr             : None
odfpy               : None
openpyxl            : None
pandas_gbq          : None
pyarrow             : None
pyreadstat          : None
pyxlsb              : None
s3fs                : None
scipy               : None
sqlalchemy          : None
tables              : None
tabulate            : None
xarray              : None
xlrd                : None
zstandard           : None
tzdata              : 2023.3
qtpy                : None
pyqt5               : None

Metadata

Metadata

Assignees

Labels

AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffDatetimeDatetime data dtypeNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions