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

fix tz deprecation #972

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandera/engines/pandas_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ def _coerce(

def _to_datetime(col: PandasObject) -> PandasObject:
col = to_datetime_fn(col, **self.to_datetime_kwargs)
if (
hasattr(pandas_dtype, "tz")
and pandas_dtype.tz is not None
and col.dt.tz is None
):
col = col.dt.tz_localize(pandas_dtype.tz)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristianmatache can we add a test case that executes this conditional? i.e. a case where the schema dtype is timezone aware but the data column isn't?

return col.astype(pandas_dtype)

if isinstance(data_container, pd.DataFrame):
Expand Down
21 changes: 11 additions & 10 deletions tests/core/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from _pytest.mark.structures import ParameterSet
from _pytest.python import Metafunc
from hypothesis import strategies as st
from pandas import DatetimeTZDtype

import pandera as pa
from pandera.engines import pandas_engine
Expand Down Expand Up @@ -388,20 +389,20 @@ def test_coerce_cast(dtypes, examples, data):
assert expected_dtype.check(pandas_engine.Engine.dtype(coerced_dtype))


dt_examples_to_coerce = [
"2021-09-01",
datetime.datetime(2021, 1, 7),
pd.NaT,
"not_a_date",
]


@pytest.mark.parametrize(
"examples, type_, failure_indices",
[
(["a", 0, "b"], int, [0, 2]),
(
[
"2021-09-01",
datetime.datetime(2021, 1, 7),
pd.NaT,
"not_a_date",
],
datetime.datetime,
[3],
),
(dt_examples_to_coerce, datetime.datetime, [3]),
(dt_examples_to_coerce, DatetimeTZDtype(tz="UTC"), [3]),
],
)
def test_try_coerce(examples, type_, failure_indices):
Expand Down