BUG: Series.searchsorted(...) fails with Timestamp DataFrames · Issue #49620 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd

df = pd.DataFrame.from_records( [ [pd.Timestamp("2002-01-11 21:00:01+0000", tz="UTC"), 4223], [pd.Timestamp("2002-01-14 21:00:01+0000", tz="UTC"), 6942], [pd.Timestamp("2002-01-15 21:00:01+0000", tz="UTC"), 12551], [pd.Timestamp("2002-01-23 21:00:01+0000", tz="UTC"), 6005], [pd.Timestamp("2002-01-29 21:00:01+0000", tz="UTC"), 2043], [pd.Timestamp("2002-02-01 21:00:01+0000", tz="UTC"), 6909], [pd.Timestamp("2002-01-14 21:00:01+0000", tz="UTC"), 5326], [pd.Timestamp("2002-01-14 21:00:01+0000", tz="UTC"), 4711], [pd.Timestamp("2002-01-22 21:00:01+0000", tz="UTC"), 103], [pd.Timestamp("2002-01-30 21:00:01+0000", tz="UTC"), 16862], [pd.Timestamp("2002-01-31 21:00:01+0000", tz="UTC"), 4143], ], columns=["time", "id"], )

This works (int data)

int_result = df.id.searchsorted(pd.DataFrame(df.id)) print(f"{int_result = }")

This fails (timestamp data)

time_result = df.time.searchsorted(pd.DataFrame(df.time)) print(f"{time_result = }")

Issue Description

Series.searchsorted has inconsistent behavior that depends on the data dtype when a DataFrame is passed as the input value. In the above code snippet, when the input DataFrame contains int data, things run successfully

int_result = array([[ 0], [11], [11], [ 1], [ 0], [ 1], [ 1], [ 1], [ 0], [11], [ 0]])

However, when the input DataFrame contains Timestamp data, things fails with TypeError: value should be a 'Timestamp', 'NaT', or array of those. Got 'StringArray' instead. (full traceback below)

Traceback (most recent call last): File "/Users/james/projects/dask/dask/test.py", line 23, in time_result = df.time.searchsorted(pd.DataFrame(df.time)) File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/pandas/core/series.py", line 3051, in searchsorted return base.IndexOpsMixin.searchsorted(self, value, side=side, sorter=sorter) File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/pandas/core/base.py", line 1296, in searchsorted return values.searchsorted(value, side=side, sorter=sorter) File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/pandas/core/arrays/_mixins.py", line 238, in searchsorted npvalue = self._validate_searchsorted_value(value) File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 781, in _validate_searchsorted_value value = self._validate_listlike(value) File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 773, in _validate_listlike raise TypeError(msg) TypeError: value should be a 'Timestamp', 'NaT', or array of those. Got 'StringArray' instead.

I should also note that if we remove the pd.DataFrame() calls above and pass Series objects to searchsorted, then both examples pass.

Expected Behavior

Based on the Series.searchsorted docstring, it's not clear to me if value being DataFrame is intended to be supported or not. If it is intended to be supported, then I'd expect Timestamp data to work in the code snippet above. If it's not intended to be supported, then I'd expect the int data example to raise an error (it currently doesn't).

Installed Versions

INSTALLED VERSIONS
------------------
commit           : 91111fd99898d9dcaa6bf6bedb662db4108da6e6
python           : 3.10.4.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 21.6.0
Version          : Darwin Kernel Version 21.6.0: Thu Sep 29 20:12:57 PDT 2022; root:xnu-8020.240.7~1/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.5.1
numpy            : 1.21.6
pytz             : 2022.1
dateutil         : 2.8.2
setuptools       : 59.8.0
pip              : 22.0.4
Cython           : None
pytest           : 7.1.3
hypothesis       : None
sphinx           : 4.5.0
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.2.0
pandas_datareader: None
bs4              : 4.11.1
bottleneck       : None
brotli           :
fastparquet      : 0.8.3
fsspec           : 2022.10.0
gcsfs            : None
matplotlib       : 3.5.1
numba            : 0.55.1
numexpr          : 2.8.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 10.0.0.dev399
pyreadstat       : None
pyxlsb           : None
s3fs             : 2022.10.0
scipy            : 1.8.1
snappy           :
sqlalchemy       : 1.4.35
tables           : 3.7.0
tabulate         : None
xarray           : 2022.3.0
xlrd             : None
xlwt             : None
zstandard        : None
tzdata           : None