Skip to content

Commit

Permalink
correct for timezone information in python datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 10, 2021
1 parent 90736cf commit 6a72b0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Descriptive stats
DataFrame.is_unique
DataFrame.n_chunks
DataFrame.null_count
DataFrame.is_empty

Computations
------------
Expand Down
6 changes: 6 additions & 0 deletions py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,12 @@ def interpolate(self) -> "DataFrame":
"""
return self.select(pl.col("*").interpolate()) # type: ignore

def is_empty(self) -> bool:
"""
Check if the dataframe is empty
"""
return self.height == 0


class GroupBy:
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/lazy/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing as tp
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Callable, Optional, Type, Union

import numpy as np
Expand Down Expand Up @@ -354,7 +354,7 @@ def lit_date(dt: datetime) -> "pl.Expr":
dt
datetime.datetime
"""
return lit(int(dt.timestamp() * 1e3))
return lit(int((dt.replace(tzinfo=timezone.utc)).timestamp() * 1e3))


def lit(
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,13 @@ def test_h_agg():
assert df.sum(axis=1, null_strategy="ignore").to_list() == [2, 2, 6]
assert df.sum(axis=1, null_strategy="propagate").to_list() == [2, None, 6]
assert df.mean(axis=1, null_strategy="propagate")[1] is None


def test_filter_date():
dataset = pl.DataFrame(
{"date": ["2020-01-02", "2020-01-03", "2020-01-04"], "index": [1, 2, 3]}
)
df = dataset.with_column(pl.col("date").str.strptime(pl.Date32, "%Y-%m-%d"))

# filter out the data to match only records from the previous year
assert df.filter(col("date") <= pl.lit_date(datetime(2019, 1, 3))).is_empty()

0 comments on commit 6a72b0e

Please sign in to comment.