Skip to content

Resampling and counting empty Series does not have a correct dtype #28427

Closed
@randomstuff

Description

@randomstuff

Code Sample, a copy-pastable example if possible

Short examples:

import pandas as pd

a = pd.Series([], dtype="datetime64[ns]", index=pd.DatetimeIndex([]))
print(a.resample("D").size().dtype) # => actual datetime64[ns], expected int64

Longer example:

import pandas as pd

t = pd.to_datetime(0)

x = pd.Series([t], index=[t])
print(x.resample("D").size().dtype) # => int64 (OK)

y = x.iloc[0:1]
print(y.resample("D").size().dtype) # => int64 (OK)

z = x.iloc[0:0]
print(z.resample("D").size().dtype) # => actual datetime64[ns], expected int64

Problem description

When .resample().size() an empty dataframe, the resulting dtype is the dtype of the initial Series. It should be an integer.

This means that this kind of things fail if the input Series happens to be empty:

monthy_rate = (x.resampe("D").size() > 0).resample("M").mean()

Expected Output

int64
int64
int64

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.4.final.0 python-bits : 64 OS : Linux OS-release : 5.2.0-2-amd64 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : fr_FR.utf8 LOCALE : fr_FR.UTF-8

pandas : 0.25.1
numpy : 1.17.2
pytz : 2019.2
dateutil : 2.8.0
pip : 18.1
setuptools : 40.8.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 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeDtype ConversionsUnexpected or buggy dtype conversionsResampleresample method

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions