Skip to content

BUG: numpy.maximum(pandas.Timestamp(...), pandas.Series(..., dtype='datetime64')) fails with TypeError #50864

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
hottwaj opened this issue Jan 19, 2023 · 6 comments · Fixed by #59338
Closed
2 of 3 tasks
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@hottwaj
Copy link

hottwaj commented Jan 19, 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, numpy
ts = pandas.Timestamp('2022-02-01')
dates_sr = pandas.to_datetime(pandas.Series(['2022-01-01', '2022-02-01', '2022-03-01']))

# note that comparison operators work
pandas.testing.assert_series_equal(ts < dates_sr, pandas.Series([False, False, True]))

# numpy.maximum fails
numpy.maximum(ts, dates_sr)

Issue Description

numpy.maximum fails when given args of type TimeStamp and Series(with dtype datetime64)

Stacktrace follows:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <module>
      2 dates_sr = pandas.to_datetime(pandas.Series(["2022-01-01", "2022-02-01", "2022-03-01"]))
      3 pandas.testing.assert_series_equal(ts < dates_sr, pandas.Series([False, False, True]))
----> 4 numpy.maximum(ts, dates_sr)

File ~/.../site-packages/pandas/core/generic.py:2032, in NDFrame.__array_ufunc__(self, ufunc, method, *inputs, **kwargs)
   2029 def __array_ufunc__(
   2030     self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any
   2031 ):
-> 2032     return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)

File ~/.../site-packages/pandas/core/arraylike.py:364, in array_ufunc(self, ufunc, method, *inputs, **kwargs)
    361 elif self.ndim == 1:
    362     # ufunc(series, ...)
    363     inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
--> 364     result = getattr(ufunc, method)(*inputs, **kwargs)
    365 else:
    366     # ufunc(dataframe)
    367     if method == "__call__" and not kwargs:
    368         # for np.<ufunc>(..) calls
    369         # kwargs cannot necessarily be handled block-by-block, so only
    370         # take this path if there are no kwargs

TypeError: '>=' not supported between instances of 'Timestamp' and 'int'

Expected Behavior

Expected return value is:

pandas.to_datetime(pandas.Series(['2022-02-01', '2022-02-01', '2022-03-01']))

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-57-generic
Version : #63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.5.3
numpy : 1.24.1
pytz : 2021.3
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.3.1
Cython : 3.0.0a10
pytest : 7.1.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.2
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : 2022.3.0
xlrd : 1.2.0
xlwt : 1.3.0
zstandard : None
tzdata : 2021.5

@hottwaj hottwaj added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 19, 2023
@rhshadrach
Copy link
Member

Thanks for the report - by calling np.maximum, pandas is doing the following:

np.maximum(ts, np.asarray(dates_sr))

This gives the same error, but no real traceback:

Traceback (most recent call last):
  File "scratch.py", line 37, in <module>
    print(np.maximum(ts, np.asarray(dates_sr)))
TypeError: '>=' not supported between instances of 'Timestamp' and 'int'

The dtype of np.asarray(dates_sr) is datetime64[ns]. So not sure where the int in the error message is coming from.

@rhshadrach rhshadrach added the datetime.date stdlib datetime.date support label Jan 19, 2023
@mroeschke
Copy link
Member

Looks like this is correctly working on main now

In [6]: import pandas, numpy
   ...: ts = pandas.Timestamp('2022-02-01')
   ...: dates_sr = pandas.to_datetime(pandas.Series(['2022-01-01', '2022-02-01', '2022-03-01']))
   ...: 
   ...: # note that comparison operators work
   ...: pandas.testing.assert_series_equal(ts < dates_sr, pandas.Series([False, False, True]))
   ...: 
   ...: # numpy.maximum fails
   ...: numpy.maximum(ts, dates_sr)
Out[6]: 
0   2022-02-01
1   2022-02-01
2   2022-03-01
dtype: datetime64[us]

I suppose we could use a test for this

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member datetime.date stdlib datetime.date support labels Jul 17, 2024
@tirthrajj
Copy link
Contributor

take

@Prabhat-Thapa45
Copy link

If it works on main shouldn't this be closed @Tirthraj93 @hottwaj ?

@rhshadrach
Copy link
Member

@Prabhat-Thapa45 - when there is a bug that happens to be fixed by other changes, we prefer to add a test so that a regression doesn't happen.

@Maverick1905
Copy link

Maverick1905 commented Jul 25, 2024

just tried the code above and receiving: No module named 'pandas._libs.pandas_parser'

and got inside of pandas:

#Below imports needs to happen first to ensure pandas top level
#module gets monkeypatched with the pandas_datetime_CAPI
#see pandas_datetime_exec in pd_datetime.c

import pandas._libs.pandas_parser # isort: skip # type: ignore[reportUnusedImport]
import pandas._libs.pandas_datetime # noqa: F401 # isort: skip # type: ignore[reportUnusedImport]
from pandas._libs.interval import Interval
from pandas._libs.tslibs import (
NaT,
NaTType,
OutOfBoundsDatetime,
Period,
Timedelta,
Timestamp,
iNaT,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants