Skip to content

Commit 028492d

Browse files
msmarchenajreback
authored andcommitted
BUG: DataFrame.asof() : Timezone Awareness / Naivety comparison TypeError (#22198)
1 parent 0850fbd commit 028492d

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ Timezones
561561
- Fixed bug where :meth:`DataFrame.describe` and :meth:`Series.describe` on tz-aware datetimes did not show `first` and `last` result (:issue:`21328`)
562562
- Bug in :class:`DatetimeIndex` comparisons failing to raise ``TypeError`` when comparing timezone-aware ``DatetimeIndex`` against ``np.datetime64`` (:issue:`22074`)
563563
- Bug in ``DataFrame`` assignment with a timezone-aware scalar (:issue:`19843`)
564+
- Bug in :func:`Dataframe.asof` that raised a ``TypeError`` when attempting to compare tz-naive and tz-aware timestamps (:issue:`21194`)
564565
- Bug when constructing a :class:`DatetimeIndex` with :class:`Timestamp`s constructed with the ``replace`` method across DST (:issue:`18785`)
565566
- Bug when setting a new value with :meth:`DataFrame.loc` with a :class:`DatetimeIndex` with a DST transition (:issue:`18308`, :issue:`20724`)
566567
- Bug in :meth:`DatetimeIndex.unique` that did not re-localize tz-aware dates correctly (:issue:`21737`)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ def asof_locs(self, where, mask):
24632463
result = np.arange(len(self))[mask].take(locs)
24642464

24652465
first = mask.argmax()
2466-
result[(locs == 0) & (where < self.values[first])] = -1
2466+
result[(locs == 0) & (where.values < self.values[first])] = -1
24672467

24682468
return result
24692469

pandas/tests/frame/test_asof.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding=utf-8
22

33
import numpy as np
4+
import pytest
45
from pandas import (DataFrame, date_range, Timestamp, Series,
56
to_datetime)
67

@@ -106,3 +107,21 @@ def test_all_nans(self):
106107
result = DataFrame(np.nan, index=[1, 2], columns=['A', 'B']).asof(3)
107108
expected = Series(np.nan, index=['A', 'B'], name=3)
108109
tm.assert_series_equal(result, expected)
110+
111+
@pytest.mark.parametrize(
112+
"stamp,expected",
113+
[(Timestamp('2018-01-01 23:22:43.325+00:00'),
114+
Series(2.0, name=Timestamp('2018-01-01 23:22:43.325+00:00'))),
115+
(Timestamp('2018-01-01 22:33:20.682+01:00'),
116+
Series(1.0, name=Timestamp('2018-01-01 22:33:20.682+01:00'))),
117+
]
118+
)
119+
def test_time_zone_aware_index(self, stamp, expected):
120+
# GH21194
121+
# Testing awareness of DataFrame index considering different
122+
# UTC and timezone
123+
df = DataFrame(data=[1, 2],
124+
index=[Timestamp('2018-01-01 21:00:05.001+00:00'),
125+
Timestamp('2018-01-01 22:35:10.550+00:00')])
126+
result = df.asof(stamp)
127+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)