Skip to content

DataFrame.quantile fails with timedelta data #18608

Closed
@mroeschke

Description

@mroeschke
>>> t1 = pd.timedelta_range('1 days', freq='D', periods=5)
>>> t2 = pd.timedelta_range('1 hours', freq='H', periods=5)
>>> df = pd.DataFrame({'t1': t1, 't2': t2})
>>> df.quantile(np.array([0.25, 0.5, 0.75]))
ValueError: need at least one array to concatenate

Problem description

This works for Series:

>>> df['t1'].quantile(np.array([0.25, 0.5, 0.75])) 
0.25   2 days
0.50   3 days
0.75   4 days
Name: t1, dtype: timedelta64[ns]

The larger issue is that the get_numeric_data in BlockManager doesn't pick up timedelta data.

The quantile issue arrises in the BlockManager here, https://github.com/pandas-dev/pandas/blob/master/pandas/core/internals.py#L3596, because I TimeDeltaBlocks have is_numeric=False. Not entire sure if is_numeric=True would be the right fix as it might have impacts downstream (one test in pandas/tests/frame/test_analytics.py failed after changing it.)

Expected Output

pd.concat([df['t1'].quantile(np.array([0.25, 0.5, 0.75])), df['t2'].quantile(np.array([0.25, 0.5, 0.75]))], axis=1) 
         t1       t2
0.25 2 days 02:00:00
0.50 3 days 03:00:00
0.75 4 days 04:00:00

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-45-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.26
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.3.2
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: 0.7.9.None
psycopg2: 2.7.1 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugInternalsRelated to non-user accessible pandas implementationTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions