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

PySeries.new_from_anyvalues loses microsecond precision #7232

Closed
2 tasks done
MarcoGorelli opened this issue Feb 27, 2023 · 1 comment · Fixed by #7268
Closed
2 tasks done

PySeries.new_from_anyvalues loses microsecond precision #7232

MarcoGorelli opened this issue Feb 27, 2023 · 1 comment · Fixed by #7268
Labels
bug Something isn't working python Related to Python Polars

Comments

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Feb 27, 2023

Polars version 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

In the below example, notice how the microseconds in the result match for the tz-naive case, but not for the tz-aware one

Reproducible example

In [62]: pl.Series([datetime(2514, 5, 30, 1, 53, 4, 986754)])
Out[62]:
shape: (1,)
Series: '' [datetime[μs]]
[
        2514-05-30 01:53:04.986754
]

In [63]: pl.Series([datetime(2514, 5, 30, 1, 53, 4, 986754, tzinfo=dt.timezone.utc)])
Out[63]:
shape: (1,)
Series: '' [datetime[μs, UTC]]
[
        2514-05-30 01:53:04.986756 UTC
]

Expected behavior

In the second case

In [63]: pl.Series([datetime(2514, 5, 30, 1, 53, 4, 986754, tzinfo=dt.timezone.utc)])
Out[63]:
shape: (1,)
Series: '' [datetime[μs, UTC]]
[
        2514-05-30 01:53:04.986754 UTC
]

The issue is that in the tz-aware case,

if dtype == Datetime and value.tzinfo is not None:
py_series = PySeries.new_from_anyvalues(name, values)
tz = str(value.tzinfo)
return pli.wrap_s(py_series).dt.replace_time_zone(tz)._s

PySeries.new_from_anyvalues is used.

And in that one, in

pub fn new_from_anyvalues(name: &str, val: Vec<Wrap<AnyValue<'_>>>) -> PyResult<PySeries> {
let avs = slice_extract_wrapped(&val);
// from anyvalues is fallible
let s = Series::from_any_values(name, avs).map_err(PyPolarsErr::from)?;
Ok(s.into())
}

avs will already have incorrectly converted to int:

[Datetime(17179869184986756, Microseconds, None)]

But that number is incorrect:

In [7]: dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(microseconds=17179869184986756)
Out[7]: datetime.datetime(2514, 5, 30, 1, 53, 4, 986756, tzinfo=datetime.timezone.utc)

It should be 17179869184986754:

In [8]: dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(microseconds=17179869184986754)
Out[8]: datetime.datetime(2514, 5, 30, 1, 53, 4, 986754, tzinfo=datetime.timezone.utc)

Installed versions

---Version info---
Polars: 0.16.6
Index type: UInt32
Platform: Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python: 3.11.1 (main, Dec  7 2022, 01:11:34) [GCC 11.3.0]
---Optional dependencies---
pyarrow: 11.0.0
pandas: 1.5.3
numpy: 1.24.1
fsspec: <not installed>
connectorx: <not installed>
xlsx2csv: <not installed>
deltalake: <not installed>
matplotlib: <not installed>
@MarcoGorelli MarcoGorelli added bug Something isn't working python Related to Python Polars labels Feb 27, 2023
@aldanor
Copy link
Contributor

aldanor commented Feb 27, 2023

Just to link it here, perhaps somewhat related or same code branches: #7212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants