Skip to content

BUG: Using pd.cut with IntervalIndex containing Timedeltas returns only NaN #47171

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
oalfred opened this issue May 30, 2022 · 5 comments
Closed
2 of 3 tasks
Labels
Bug cut cut, qcut Duplicate Report Duplicate issue or pull request Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@oalfred
Copy link

oalfred commented May 30, 2022

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

one_to_seven_days_timedeltas = [pd.Timedelta(days=d) for d in [1, 2, 3, 4, 5, 6, 7]]
even_days_timedeltas = [pd.Timedelta(days=d) for d in [0, 2, 4, 6, 8]]

s = pd.Series(one_to_seven_days_timedeltas)
i = pd.IntervalIndex.from_breaks(even_days_timedeltas, closed='left')

print(pd.cut(s, i))

Issue Description

Using pd.cut when the bins are an IntervalIndex that contains Timedelta returns only NaN. Output from the example is

0    NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
6    NaN
dtype: category
Categories (4, interval[timedelta64[ns], left]): [ <
                                                  [0 days 00:00:00, 2 days 00:00:00) <
                                                  [2 days 00:00:00, 4 days 00:00:00) <
                                                  [4 days 00:00:00, 6 days 00:00:00) <
                                                  [6 days 00:00:00, 8 days 00:00:00)]

I believe the problem might be that the data, s in the example, is converted to a numeric data type but the bins are not.

Expected Behavior

The data should be binned according to the intervals in the IntervalIndex. Output from the example should be

0    [0 days 00:00:00, 2 days 00:00:00)
1    [2 days 00:00:00, 4 days 00:00:00)
2    [2 days 00:00:00, 4 days 00:00:00)
3    [4 days 00:00:00, 6 days 00:00:00)
4    [4 days 00:00:00, 6 days 00:00:00)
5    [6 days 00:00:00, 8 days 00:00:00)
6    [6 days 00:00:00, 8 days 00:00:00)
dtype: category
Categories (4, interval[timedelta64[ns], left]): [ <
                                                  [0 days 00:00:00, 2 days 00:00:00) <
                                                  [2 days 00:00:00, 4 days 00:00:00) <
                                                  [4 days 00:00:00, 6 days 00:00:00) <
                                                  [6 days 00:00:00, 8 days 00:00:00)]

Installed Versions

Running pd.show_versions() fails on assert '_distutils' in core.__file__, core.__file__ in .../_distutils_hack/__init__.py:59 which is quite confusing but I was able to get around this by setting the environment variable SETUPTOOLS_USE_DISTUTILS=stdlib.

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.10.4.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 61.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.1
IPython : 8.2.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : 0.8.1
fsspec : 2022.3.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.35
tables : 3.7.0
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@oalfred oalfred added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 30, 2022
@iansheng
Copy link
Contributor

Pandas 1.3.5 behaves as expected, this problem has appeared since 1.4.0.

@doodger
Copy link

doodger commented Jun 2, 2022

Same issue has happened to me on Pandas 1.4.2; using cut and groupby with time intervals works as expected on pandas 1.3.4

@simonjayhawkins
Copy link
Member

Thanks @oalfred for the report. This looks like a duplicate of #46218.

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version cut cut, qcut and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2022
@simonjayhawkins simonjayhawkins added this to the 1.4.3 milestone Jun 6, 2022
@simonjayhawkins simonjayhawkins added the Duplicate Report Duplicate issue or pull request label Jun 6, 2022
@simonjayhawkins
Copy link
Member

moving to 1.4.4

@simonjayhawkins simonjayhawkins modified the milestones: 1.4.3, 1.4.4 Jun 22, 2022
@mroeschke
Copy link
Member

Closing as a duplicate of #46218. Discussion of a solution seems further along there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug cut cut, qcut Duplicate Report Duplicate issue or pull request Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

5 participants