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.Date should return a datetime #13598

Open
2 tasks done
mcrumiller opened this issue Jan 10, 2024 · 0 comments · May be fixed by #15679
Open
2 tasks done

mean and median for pl.Date should return a datetime #13598

mcrumiller opened this issue Jan 10, 2024 · 0 comments · May be fixed by #15679
Labels
bug Something isn't working P-medium Priority: medium python Related to Python Polars

Comments

@mcrumiller
Copy link
Contributor

mcrumiller commented Jan 10, 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.

Reproducible example

from datetime import date
import polars as pl

s = pl.Series([date(2024, 1, 1), date(2024, 1, 2)])
print("mean:")
print(s.mean())
print(s.dt.mean())
print(s.to_frame().select(pl.all().mean()))

print("\nmedian:")
print(s.median())
print(s.dt.median())
print(s.to_frame().select(pl.all().median()))

Log output

mean:
19723.5
2024-01-01
shape: (1, 1)
┌──────┐
│      │
│ ---  │
│ date │
╞══════╡
│ null │
└──────┘

median:
19723.5
2024-01-01
polars.exceptions.InvalidOperationError: `median` operation not supported for dtype `date`

Issue description

dt.mean() returns a date and mean() returns a float or null, depending on context, when the input has dtype pl.Date. Median similarly has inconsistent behavior.

Using the analogy for integers and real numbers, the mean and median of dates should be a datetime, similar to how the mean and median of an integers is a float:

mean(1, 2) = 1.5
mean(2024-01-01, 2024-01-02) = 2024-01-01 12:00:00

Expected behavior

Returns a datetime that is at the average (mean or median) time point, and is consistent across uses of mean and median.

Implementing this for the remaining temporal types (in a future issue) will enable the deprecation of .dt.mean and .dt.median, as those will become redundant with series.mean and series.median when the datatype is temporal.

Installed versions

--------Version info---------
Polars:               0.20.3
Index type:           UInt32
Platform:             Windows-10-10.0.22621-SP0
Python:               3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          3.0.0
connectorx:           0.3.2
deltalake:            0.15.1
fsspec:               2023.12.2
gevent:               23.9.1
hvplot:               0.9.1
matplotlib:           3.8.2
numpy:                1.26.3
openpyxl:             3.1.2
pandas:               2.1.4
pyarrow:              14.0.2
pydantic:             2.5.3
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 python Related to Python Polars labels Jan 10, 2024
@stinodego stinodego added the accepted Ready for implementation label Jan 10, 2024
@stinodego stinodego added P-medium Priority: medium and removed accepted Ready for implementation labels Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-medium Priority: medium python Related to Python Polars
Projects
Status: Ready
2 participants