Skip to content

Commit 793ac8b

Browse files
authored
BUG: .asof casting bool results to float64 GH#16063 (#45299)
1 parent ceedfa9 commit 793ac8b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

doc/source/whatsnew/v1.5.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ Styler
258258

259259
Other
260260
^^^^^
261+
- Bug in :meth:`Series.asof` and :meth:`DataFrame.asof` incorrectly casting bool-dtype results to ``float64`` dtype (:issue:`16063`)
262+
-
261263

262264
.. ***DO NOT USE THIS SECTION***
263265

pandas/core/generic.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -7133,9 +7133,9 @@ def asof(self, where, subset=None):
71337133
>>> df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',
71347134
... '2018-02-27 09:04:30']),
71357135
... subset=['a'])
7136-
a b
7137-
2018-02-27 09:03:30 30.0 NaN
7138-
2018-02-27 09:04:30 40.0 NaN
7136+
a b
7137+
2018-02-27 09:03:30 30 NaN
7138+
2018-02-27 09:04:30 40 NaN
71397139
"""
71407140
if isinstance(where, str):
71417141
where = Timestamp(where)
@@ -7205,7 +7205,10 @@ def asof(self, where, subset=None):
72057205
missing = locs == -1
72067206
data = self.take(locs)
72077207
data.index = where
7208-
data.loc[missing] = np.nan
7208+
if missing.any():
7209+
# GH#16063 only do this setting when necessary, otherwise
7210+
# we'd cast e.g. bools to floats
7211+
data.loc[missing] = np.nan
72097212
return data if is_list else data.iloc[-1]
72107213

72117214
# ----------------------------------------------------------------------

pandas/tests/frame/methods/test_asof.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def test_subset(self, date_range_frame):
6868
result = df.asof(dates, subset="B")
6969
expected = df.resample("25s", closed="right").ffill().reindex(dates)
7070
expected.iloc[20:] = 9
71+
# no "missing", so "B" can retain int dtype (df["A"].dtype platform-dependent)
72+
expected["B"] = expected["B"].astype(df["B"].dtype)
7173

7274
tm.assert_frame_equal(result, expected)
7375

@@ -135,11 +137,11 @@ def test_all_nans(self, date_range_frame):
135137
[
136138
(
137139
Timestamp("2018-01-01 23:22:43.325+00:00"),
138-
Series(2.0, name=Timestamp("2018-01-01 23:22:43.325+00:00")),
140+
Series(2, name=Timestamp("2018-01-01 23:22:43.325+00:00")),
139141
),
140142
(
141143
Timestamp("2018-01-01 22:33:20.682+01:00"),
142-
Series(1.0, name=Timestamp("2018-01-01 22:33:20.682+01:00")),
144+
Series(1, name=Timestamp("2018-01-01 22:33:20.682+01:00")),
143145
),
144146
],
145147
)
@@ -180,3 +182,14 @@ def test_asof_periodindex_mismatched_freq(self):
180182
msg = "Input has different freq"
181183
with pytest.raises(IncompatibleFrequency, match=msg):
182184
df.asof(rng.asfreq("D"))
185+
186+
def test_asof_preserves_bool_dtype(self):
187+
# GH#16063 was casting bools to floats
188+
dti = date_range("2017-01-01", freq="MS", periods=4)
189+
ser = Series([True, False, True], index=dti[:-1])
190+
191+
ts = dti[-1]
192+
res = ser.asof([ts])
193+
194+
expected = Series([True], index=[ts])
195+
tm.assert_series_equal(res, expected)

0 commit comments

Comments
 (0)