Skip to content

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

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
randomstuff opened this issue Sep 13, 2019 · 3 comments · Fixed by #28459
Closed

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

randomstuff opened this issue Sep 13, 2019 · 3 comments · Fixed by #28459
Labels
Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions Resample resample method
Milestone

Comments

@randomstuff
Copy link
Contributor

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

@randomstuff
Copy link
Contributor Author

randomstuff commented Sep 13, 2019

Possibly related to #4962 or #26411

@TomAugspurger
Copy link
Contributor

Confirmed, thanks for the bug report.

Not sure where this would be going wrong.

@TomAugspurger TomAugspurger added Dtype Conversions Unexpected or buggy dtype conversions Resample resample method Datetime Datetime data dtype labels Sep 13, 2019
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Sep 13, 2019
@randomstuff
Copy link
Contributor Author

randomstuff commented Sep 13, 2019

This is coming from this:

    def _downsample(self, how, **kwargs):
        """
        Downsample the cython defined function.

        Parameters
        ----------
        how : string / cython mapped function
        **kwargs : kw args passed to how function
        """
        self._set_binner()
        how = self._get_cython_func(how) or how
        ax = self.ax
        obj = self._selected_obj

        if not len(ax):  # <= This returns the wrong type in this case.
            # reset to the new freq
            obj = obj.copy()
            obj.index.freq = self.freq
            return obj

        # do we have a regular frequency
        if ax.freq is not None or ax.inferred_freq is not None:

            if len(self.grouper.binlabels) > len(ax) and how is None:

                # let's do an asfreq
                return self.asfreq()

        # we are downsampling
        # we want to call the actual grouper method here
        result = obj.groupby(self.grouper, axis=self.axis).aggregate(how, **kwargs)

        result = self._apply_loffset(result)
        return self._wrap_result(result)

randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 17, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 17, 2019
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Sep 17, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 17, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Sep 19, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Oct 9, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Oct 9, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Oct 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Oct 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Oct 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Nov 16, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Nov 19, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Nov 19, 2019
randomstuff pushed a commit to randomstuff/pandas that referenced this issue Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions Resample resample method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants