Skip to content

BUG: Fix localize_pydatetime using meta datetimes as Timestamp (#25734) #25746

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Numeric
Conversion
^^^^^^^^^^

-
- Bug in :meth:`localize_pydatetime` where some ``datetime`` were incorrectly treated as pandas :class:`Timestamp` (:issue:`25734`)
-
-

Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ from pandas._libs.tslibs.np_datetime cimport (
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

from pandas._libs.tslibs.util cimport (
is_string_object, is_datetime64_object, is_integer_object, is_float_object)
is_string_object, is_datetime64_object, is_integer_object, is_float_object,
is_timestamp)

from pandas._libs.tslibs.timedeltas cimport (cast_from_unit,
delta_to_nanoseconds)
Expand Down Expand Up @@ -607,8 +608,7 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz):
"""
if tz is None:
return dt
elif not PyDateTime_CheckExact(dt):
# i.e. is a Timestamp
elif is_timestamp(dt):
return dt.tz_localize(tz)
elif is_utc(tz):
return _localize_pydatetime(dt, tz)
Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def round_nsint64(values, mode, freq):
# shadows the python class, where we do any heavy lifting.
cdef class _Timestamp(datetime):

_typ = 'timestamp'

cdef readonly:
int64_t value, nanosecond
object freq # frequency reference
Expand Down
15 changes: 15 additions & 0 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,18 @@ cdef inline bint is_nan(object val):
is_nan : bool
"""
return (is_float_object(val) or is_complex_object(val)) and val != val


cdef inline bint is_timestamp(object val):
"""
Cython equivalent of `isinstance(val, pd.Timestamp)`

Parameters
----------
val : object

Returns
-------
is_timestamp : bool
"""
return getattr(val, '_typ', None) == "timestamp"
26 changes: 26 additions & 0 deletions pandas/tests/tslibs/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import numpy as np
import pytest
from pytz import UTC
from datetime import datetime

from pandas._libs.tslib import iNaT
from pandas._libs.tslibs import conversion, timezones

from pandas import date_range
import pandas.util.testing as tm

from pandas import Timestamp


def _compare_utc_to_local(tz_didx):
def f(x):
Expand Down Expand Up @@ -66,3 +69,26 @@ def test_length_zero_copy(dtype, copy):
arr = np.array([], dtype=dtype)
result = conversion.ensure_datetime64ns(arr, copy=copy)
assert result.base is (None if copy else arr)


class MetaDatetime(type):
pass


class FakeDatetime(MetaDatetime("NewBase", (datetime,), {})):
pass


@pytest.mark.parametrize("dt, expected", [
pytest.param(Timestamp("2000-01-01"),
Timestamp("2000-01-01", tz=UTC), id="timestamp"),
pytest.param(datetime(2000, 1, 1),
datetime(2000, 1, 1, tzinfo=UTC),
id="datetime"),
pytest.param(FakeDatetime(2000, 1, 1),
FakeDatetime(2000, 1, 1, tzinfo=UTC),
id="fakedatetime")])
def test_localize_pydatetime_dt_types(dt, expected):
# GH 25734
result = conversion.localize_pydatetime(dt, UTC)
assert result == expected