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

mean and median for pl.Duration should return a duration #14375

Closed
2 tasks done
mcrumiller opened this issue Feb 8, 2024 · 0 comments · Fixed by #14376
Closed
2 tasks done

mean and median for pl.Duration should return a duration #14375

mcrumiller opened this issue Feb 8, 2024 · 0 comments · Fixed by #14376
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-medium Priority: medium python Related to Python Polars

Comments

@mcrumiller
Copy link
Contributor

mcrumiller commented Feb 8, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Issue description

Part of #13599. mean and median works in expression and groupby contexts but not Series contexts.

from datetime import timedelta
import polars as pl
s = pl.Series([timedelta(days=1), timedelta(days=2), timedelta(days=15)])
df = pl.DataFrame({
    "key": [1, 1, 1],
    "s": s
})

Series

print(s.mean())
print(s.median())
print(s.dt.mean())
print(s.dt.median())
518400000000.0
172800000000.0
1970-01-07 00:00:00  # yikes
1970-01-03 00:00:00

Frame

print(
    df.select(
        col("s").mean().alias("mean"),
        col("s").median().alias("median")
    )
)
shape: (1, 2)
┌──────────────┬──────────────┐
│ mean         ┆ median       │
│ ---          ┆ ---          │
│ duration[μs] ┆ duration[μs] │
╞══════════════╪══════════════╡
│ 6d           ┆ 2d           │
└──────────────┴──────────────┘

Groupby

print(
    df.group_by("key").agg(
        col("s").mean().alias("mean"),
        col("s").median().alias("median")
    )
)
shape: (1, 3)
┌─────┬──────────────┬──────────────┐
│ key ┆ mean         ┆ median       │
│ --- ┆ ---          ┆ ---          │
│ i64 ┆ duration[μs] ┆ duration[μs] │
╞═════╪══════════════╪══════════════╡
│ 1   ┆ 6d           ┆ 2d           │
└─────┴──────────────┴──────────────┘

Installed versions

--------Version info---------
Polars:               0.20.7
Index type:           UInt32
Platform:             Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python:               3.12.1 (main, Jan 31 2024, 09:51:46) [GCC 11.4.0]

----Optional dependencies----
adbc_driver_manager:  0.9.0
cloudpickle:          3.0.0
connectorx:           <not installed>
deltalake:            0.15.2
fsspec:               2023.12.2
gevent:               23.9.1
hvplot:               0.9.2
matplotlib:           3.8.2
numpy:                1.26.4
openpyxl:             3.1.2
pandas:               2.2.0
pyarrow:              15.0.0
pydantic:             2.6.1
pyiceberg:            0.5.1
pyxlsb:               1.0.10
sqlalchemy:           2.0.25
xlsx2csv:             0.8.1
xlsxwriter:           3.1.9
@mcrumiller mcrumiller added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Feb 8, 2024
@stinodego stinodego added P-medium Priority: medium A-timeseries Area: date/time functionality and removed needs triage Awaiting prioritization by a maintainer labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-medium Priority: medium python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants