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

qcut raising ValueError if NaT present #19768

Closed
gulp21 opened this Issue Feb 19, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@gulp21

gulp21 commented Feb 19, 2018

Code Sample, a copy-pastable example if possible

from io import StringIO
import pandas as pd

csv = 'Index,Date\n1,2013-01-01 23:00:00\n2,\n3,2013-01-01 23:00:01'
df = pd.read_csv(StringIO(csv), index_col=0, parse_dates=[1])
pd.qcut(df["Date"], 2)

Problem description

qcut raises a ValueError:

Traceback (most recent call last):
  File "mve.py", line 26, in <module>
    pd.qcut(df["Date"], 2)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/reshape/tile.py", line 208, in qcut
    dtype=dtype, duplicates=duplicates)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/reshape/tile.py", line 251, in _bins_to_cuts
    dtype=dtype)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/reshape/tile.py", line 344, in _format_labels
    labels = IntervalIndex.from_breaks(breaks, closed=closed)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/indexes/interval.py", line 370, in from_breaks
    name=name, copy=copy)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/indexes/interval.py", line 411, in from_arrays
    copy=copy, verify_integrity=True)
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/indexes/interval.py", line 225, in _simple_new
    result._validate()
  File "/tmp/test/env/lib/python3.5/site-packages/pandas/core/indexes/interval.py", line 265, in _validate
    raise ValueError('missing values must be missing in the same '
ValueError: missing values must be missing in the same location both left and right sides

Expected Output

qcut returning something like

Index
1    (2013-01-01 22:59:59.999999999, 2013-01-01 23:00:01.0
2    NaT
3    (2013-01-01 22:59:59.999999999, 2013-01-01 23:00:01.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-32-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8

pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 38.5.1
Cython: None
numpy: 1.14.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@gfyoung

This comment has been minimized.

Member

gfyoung commented Feb 21, 2018

  1. Could you rewrite your example to not use read_csv (i.e. just construct the DataFrame from scratch).

  2. Yeah...that does look weird indeed. Even the error message is confusing. PR to patch is welcome!

@jschendel

This comment has been minimized.

Member

jschendel commented Feb 22, 2018

Issue looks to occur in the integer conversion here:

elif is_datetime64_dtype(x):
x = to_datetime(x).view(np.int64)

This converts NaT to a negative integer value:

In [2]: dti = pd.DatetimeIndex(['20180101', pd.NaT, '20180103'])

In [3]: dti
Out[3]: DatetimeIndex(['2018-01-01', 'NaT', '2018-01-03'], dtype='datetime64[ns]', freq=None)

In [4]: dti.view(np.int64)
Out[4]: array([ 1514764800000000000, -9223372036854775808,  1514937600000000000], dtype=int64)

So NaT is getting passed through as numeric, then looks to be assigned as the end point of an interval in the output after getting converted back to NaT, which is an invalid way to construct intervals.

@jschendel jschendel referenced this issue Feb 22, 2018

Merged

BUG: Fix qcut with NaT present #19833

4 of 4 tasks complete

@jreback jreback added this to the 0.23.0 milestone Feb 22, 2018

@jreback jreback added Bug and removed Error Reporting labels Feb 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment