-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 3 commits
2c4ee4d
de72ec6
f585ecd
194caf5
2de4d15
849493c
c773407
27657f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from datetime import datetime, timedelta | ||
from itertools import product, starmap | ||
import operator | ||
import sys | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -21,6 +22,7 @@ | |
DatetimeIndex, NaT, Period, Series, Timedelta, TimedeltaIndex, Timestamp, | ||
date_range) | ||
from pandas.core.indexes.datetimes import _to_M8 | ||
from pandas.tseries.offsets import CustomBusinessDay | ||
import pandas.util.testing as tm | ||
|
||
|
||
|
@@ -2350,3 +2352,34 @@ def test_shift_months(years, months): | |
for x in dti] | ||
expected = DatetimeIndex(raw) | ||
tm.assert_index_equal(actual, expected) | ||
|
||
|
||
def test_add_with_monkeypatched_datetime(monkeypatch): | ||
# GH 25734 | ||
|
||
class MetaDatetime(type): | ||
@classmethod | ||
def __instancecheck__(self, obj): | ||
return isinstance(obj, datetime) | ||
|
||
class FakeDatetime(MetaDatetime("NewBase", (datetime,), {})): | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A metaclass is needed in this test case to be able to override |
||
|
||
with monkeypatch.context() as m: | ||
# monkeypatch datetime everywhere | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you expand this comment to describe what |
||
for mod_name, module in list(sys.modules.items()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least some level of monkeypatching is necessary for the original issue to show up. However, this entire |
||
if (mod_name == __name__ or | ||
module.__name__ in ('datetime',)): | ||
continue | ||
for attribute_name in dir(module): | ||
try: | ||
attribute_value = getattr(module, attribute_name) | ||
except (ImportError, AttributeError, TypeError): | ||
continue | ||
if id(datetime) == id(attribute_value): | ||
m.setattr(module, attribute_name, FakeDatetime) | ||
|
||
dt = FakeDatetime(2000, 1, 1, tzinfo=pytz.UTC) | ||
result = Timestamp(dt) + CustomBusinessDay() | ||
expected = Timestamp("2000-01-03", tzinfo=pytz.UTC) | ||
assert result == expected |
Uh oh!
There was an error while loading. Please reload this page.