Skip to content
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

ENH: Support "std" aggregation in groupby for groupby datetime column #48481

Closed
3 tasks done
PrimozGodec opened this issue Sep 9, 2022 · 3 comments · Fixed by #51333
Closed
3 tasks done

ENH: Support "std" aggregation in groupby for groupby datetime column #48481

PrimozGodec opened this issue Sep 9, 2022 · 3 comments · Fixed by #51333
Labels

Comments

@PrimozGodec
Copy link

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

df = pd.DataFrame(
    [
        [1, "2022-02-02"],
        [1, "2022-03-02"],
        [1, "2022-03-02"],
        [2, "2022-04-02"],
        [2, "2022-04-02"],
        [2, "2022-05-02"],
    ], columns=["a", "dt"]
)
df["dt"] = pd.to_datetime(df["dt"])
gb = df.groupby("a")
print(gb["dt"].agg(lambda d: d.std()))
print(gb["dt"].agg("std"))

Issue Description

Computing standard deviation fails on GroupBy object, but it is successful with the std method on DataFrame.

Traceback (most recent call last):
  File "example.py", line 16, in <module>
    print(gb["dt"].agg("std"))
  File "pandas/pandas/core/groupby/generic.py", line 276, in aggregate
    return getattr(self, func)(*args, **kwargs)
  File "pandas/pandas/core/groupby/groupby.py", line 2261, in std
    result = self._get_cythonized_result(
  File "pandas/pandas/core/groupby/groupby.py", line 3818, in _get_cythonized_result
    res_mgr = mgr.grouped_reduce(blk_func, ignore_failures=True)
  File "pandas/core/internals/base.py", line 199, in grouped_reduce
    res = func(arr)
  File "pandas/pandas/core/groupby/groupby.py", line 3778, in blk_func
    vals = vals.astype(cython_dtype, copy=False)
  File "pandas/pandas/core/arrays/datetimes.py", line 636, in astype
    return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)
  File "pandas/pandas/core/arrays/datetimelike.py", line 507, in astype
    raise TypeError(msg)
TypeError: Cannot cast DatetimeArray to dtype float64

Expected Behavior

Computing standard deviation on GroupBy should be successful.

Installed Versions

Tested on master and released version. Failed in both cases.

INSTALLED VERSIONS

commit : 047c11d
python : 3.10.4.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 1.6.0.dev0+89.g047c11d780
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : 5.1.1
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : 0.56.0
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : 1.4.39
tables : None
tabulate : 0.8.10
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

@PrimozGodec PrimozGodec added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 9, 2022
@phofl
Copy link
Member

phofl commented Sep 9, 2022

Hi, thanks for your report. We don't have support for date time columns in our groupby var/std algos

@PrimozGodec
Copy link
Author

PrimozGodec commented Sep 9, 2022

Ok so if understand corectly it is not a issue and I should just aggregate with DataFrame's std: gb["dt"].agg(lambda d: d.std()) in case of time variable?

@phofl
Copy link
Member

phofl commented Sep 9, 2022

It would be more of an enhancement request than a bug imo

@mroeschke mroeschke changed the title BUG: "std" aggregation fails on groupby for datetime column ENH: Support "std" aggregation in groupby for groupby datetime column Sep 9, 2022
@mroeschke mroeschke added Enhancement Groupby Reduction Operations sum, mean, min, max, etc. and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants