Skip to content

Commit

Permalink
add microseconds conversion test (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 13, 2022
1 parent 44e1fdf commit 6d791bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Binary file removed py-polars/null.parquet
Binary file not shown.
9 changes: 5 additions & 4 deletions py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ def _prepare_row_count_args(
return None


EPOCH = datetime(1970, 1, 1).replace(tzinfo=None)


def _to_python_datetime(
value: Union[int, float], dtype: Type[DataType], tu: Optional[str] = "ns"
) -> Union[date, datetime]:
print(tu, value)
if dtype == Date:
# days to seconds
# important to create from utc. Not doing this leads
Expand All @@ -176,10 +178,9 @@ def _to_python_datetime(
elif dtype == Datetime:
if tu == "ns":
# nanoseconds to seconds
return datetime.utcfromtimestamp(value / 1_000_000_000)
return EPOCH + timedelta(microseconds=value / 1000)
if tu == "us":
# microseconds to seconds
return datetime.utcfromtimestamp(value / 1_000_000)
return EPOCH + timedelta(microseconds=value)
elif tu == "ms":
# milliseconds to seconds
return datetime.utcfromtimestamp(value / 1_000)
Expand Down
5 changes: 3 additions & 2 deletions py-polars/tests/lazy_io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def test_categorical_parquet_statistics() -> None:


def test_null_parquet() -> None:
file = path.join(path.dirname(__file__), "null.parquet")
df = pl.DataFrame([pl.Series("foo", [], dtype=pl.Int8)])
df.to_parquet("null.parquet")
out = pl.read_parquet("null.parquet")
df.to_parquet(file)
out = pl.read_parquet(file)
assert out.frame_equal(df)
18 changes: 18 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,21 @@ def test_upsample() -> None:
)

assert up.frame_equal(expected)


def test_microseconds_accuracy() -> None:
timestamps = [
datetime(2600, 1, 1, 0, 0, 0, 123456),
datetime(2800, 1, 1, 0, 0, 0, 456789),
]
a = pa.Table.from_arrays(
arrays=[timestamps, [128, 256]],
schema=pa.schema(
[
("timestamp", pa.timestamp("us")),
("value", pa.int16()),
]
),
)

assert pl.from_arrow(a)["timestamp"].to_list() == timestamps # type: ignore

0 comments on commit 6d791bd

Please sign in to comment.