Skip to content

CLN: remove unnecessary casting from Block methods #38700

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

Merged
merged 1 commit into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 0 additions & 12 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
find_common_type,
infer_dtype_from,
infer_dtype_from_scalar,
maybe_box_datetimelike,
maybe_downcast_numeric,
maybe_downcast_to_dtype,
maybe_infer_dtype_type,
Expand Down Expand Up @@ -746,11 +745,6 @@ def replace(
return [self] if inplace else [self.copy()]

values = self.values
if lib.is_scalar(to_replace) and isinstance(values, np.ndarray):
# The only non-DatetimeLike class that also has a non-trivial
# try_coerce_args is ObjectBlock, but that overrides replace,
# so does not get here.
to_replace = convert_scalar_for_putitemlike(to_replace, values.dtype)

mask = missing.mask_missing(values, to_replace)
if not mask.any():
Expand Down Expand Up @@ -845,7 +839,6 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
if isna(s):
return ~mask

s = maybe_box_datetimelike(s)
return compare_or_regex_search(self.values, s, regex, mask)

if self.is_object:
Expand Down Expand Up @@ -919,8 +912,6 @@ def setitem(self, indexer, value):
arr = self.array_values().T
arr[indexer] = value
return self
elif lib.is_scalar(value):
value = convert_scalar_for_putitemlike(value, values.dtype)

else:
# current dtype cannot store value, coerce to common dtype
Expand Down Expand Up @@ -1067,9 +1058,6 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
arr.putmask(mask, new)
return [self]

if lib.is_scalar(new):
new = convert_scalar_for_putitemlike(new, self.values.dtype)

if transpose:
new_values = new_values.T

Expand Down
57 changes: 35 additions & 22 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_fillna_downcast(self):
expected = Series([1, 0])
tm.assert_series_equal(result, expected)

def test_timedelta_fillna(self):
def test_timedelta_fillna(self, frame_or_series):
# GH#3371
ser = Series(
[
Expand All @@ -188,9 +188,10 @@ def test_timedelta_fillna(self):
]
)
td = ser.diff()
obj = frame_or_series(td)

# reg fillna
result = td.fillna(Timedelta(seconds=0))
result = obj.fillna(Timedelta(seconds=0))
expected = Series(
[
timedelta(0),
Expand All @@ -199,13 +200,14 @@ def test_timedelta_fillna(self):
timedelta(days=1, seconds=9 * 3600 + 60 + 1),
]
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

# interpreted as seconds, deprecated
with pytest.raises(TypeError, match="Passing integers to fillna"):
td.fillna(1)
obj.fillna(1)

result = td.fillna(Timedelta(seconds=1))
result = obj.fillna(Timedelta(seconds=1))
expected = Series(
[
timedelta(seconds=1),
Expand All @@ -214,9 +216,10 @@ def test_timedelta_fillna(self):
timedelta(days=1, seconds=9 * 3600 + 60 + 1),
]
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

result = td.fillna(timedelta(days=1, seconds=1))
result = obj.fillna(timedelta(days=1, seconds=1))
expected = Series(
[
timedelta(days=1, seconds=1),
Expand All @@ -225,9 +228,10 @@ def test_timedelta_fillna(self):
timedelta(days=1, seconds=9 * 3600 + 60 + 1),
]
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

result = td.fillna(np.timedelta64(int(1e9)))
result = obj.fillna(np.timedelta64(int(1e9)))
expected = Series(
[
timedelta(seconds=1),
Expand All @@ -236,9 +240,10 @@ def test_timedelta_fillna(self):
timedelta(days=1, seconds=9 * 3600 + 60 + 1),
]
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

result = td.fillna(NaT)
result = obj.fillna(NaT)
expected = Series(
[
NaT,
Expand All @@ -248,21 +253,27 @@ def test_timedelta_fillna(self):
],
dtype="m8[ns]",
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

# ffill
td[2] = np.nan
result = td.ffill()
obj = frame_or_series(td)
result = obj.ffill()
expected = td.fillna(Timedelta(seconds=0))
expected[0] = np.nan
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)

tm.assert_equal(result, expected)

# bfill
td[2] = np.nan
result = td.bfill()
obj = frame_or_series(td)
result = obj.bfill()
expected = td.fillna(Timedelta(seconds=0))
expected[2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

def test_datetime64_fillna(self):

Expand Down Expand Up @@ -553,7 +564,7 @@ def test_fillna_period(self):
tm.assert_series_equal(res, exp)
assert res.dtype == "Period[M]"

def test_fillna_dt64_timestamp(self):
def test_fillna_dt64_timestamp(self, frame_or_series):
ser = Series(
[
Timestamp("20130101"),
Expand All @@ -563,9 +574,10 @@ def test_fillna_dt64_timestamp(self):
]
)
ser[2] = np.nan
obj = frame_or_series(ser)

# reg fillna
result = ser.fillna(Timestamp("20130104"))
result = obj.fillna(Timestamp("20130104"))
expected = Series(
[
Timestamp("20130101"),
Expand All @@ -574,11 +586,12 @@ def test_fillna_dt64_timestamp(self):
Timestamp("20130103 9:01:01"),
]
)
tm.assert_series_equal(result, expected)
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

result = ser.fillna(NaT)
expected = ser
tm.assert_series_equal(result, expected)
result = obj.fillna(NaT)
expected = obj
tm.assert_equal(result, expected)

def test_fillna_dt64_non_nao(self):
# GH#27419
Expand Down