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

Closed
2 tasks done
mcrumiller opened this issue Jan 10, 2024 · 3 comments · Fixed by #16795
Closed
2 tasks done

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

mcrumiller opened this issue Jan 10, 2024 · 3 comments · Fixed by #16795
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
@johnros
Copy link

johnros commented Jun 14, 2024

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

I am not sure the analogy is correct. I think it may depend on the use-case. There are cases one would want to get the "typical date" and sometimes the "point in time that is the center of the dataset".

From a mathematical viewpoint, the Frechet mean in the space of dates, is a date. But this formalism may be an overkill, I thus think the desired behavior depends on the use-case.

@mcrumiller
Copy link
Contributor Author

@johnros it would definitely be simpler implementation-wise to return a date. Nobody was previously opposed to the new implementation, which made more sense to me, and the analogy seems apt. I'm willing to backtrack and return to a simpler date implementation if others agree.

@johnros
Copy link

johnros commented Jun 14, 2024

I am certainly no decision maker here. My intuition (lacking a concrete use-case), is that the mean and median of dates should be a date. Curious to hear others' opinions.

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
Archived in project
3 participants