Skip to content

Commit 76389b2

Browse files
committed
timedelta fillna check
1 parent b4cf203 commit 76389b2

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

pandas/core/internals.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
371371
else:
372372
return self.copy()
373373

374-
original_value = value
375374
mask = isnull(self.values)
376375
if limit is not None:
377376
if not is_integer(limit):
@@ -398,13 +397,15 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
398397

399398
# operate column-by-column
400399
def f(m, v, i):
400+
block = self.coerce_to_target_dtype(value)
401401

402-
# try again with a compatible block
403-
block = self.coerce_to_target_dtype(original_value)
404-
return block.fillna(original_value,
402+
# slice out our block
403+
if i is not None:
404+
block = block.getitem_block(slice(i, i + 1))
405+
return block.fillna(value,
405406
limit=limit,
406407
inplace=inplace,
407-
downcast=False)
408+
downcast=None)
408409

409410
return self.split_and_operate(mask, f, inplace)
410411

@@ -962,13 +963,11 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
962963
# operate column-by-column
963964
def f(m, v, i):
964965

965-
# TODO(jreback)
966-
# see if we can use coerce_to_target_dtype here instead
967-
968966
if i is None:
969967
# ndim==1 case.
970968
n = new
971969
else:
970+
972971
if isinstance(new, np.ndarray):
973972
n = np.squeeze(new[i % new.shape[0]])
974973
else:
@@ -1886,7 +1885,7 @@ def fillna(self, value, **kwargs):
18861885

18871886
# allow filling with integers to be
18881887
# interpreted as seconds
1889-
if not isinstance(value, np.timedelta64):
1888+
if is_integer(value) and not isinstance(value, np.timedelta64):
18901889
value = Timedelta(value, unit='s')
18911890
return super(TimeDeltaBlock, self).fillna(value, **kwargs)
18921891

@@ -1917,11 +1916,11 @@ def _try_coerce_args(self, values, other):
19171916
elif isinstance(other, Timedelta):
19181917
other_mask = isnull(other)
19191918
other = other.value
1919+
elif isinstance(other, timedelta):
1920+
other = Timedelta(other).value
19201921
elif isinstance(other, np.timedelta64):
19211922
other_mask = isnull(other)
19221923
other = Timedelta(other).value
1923-
elif isinstance(other, timedelta):
1924-
other = Timedelta(other).value
19251924
elif hasattr(other, 'dtype') and is_timedelta64_dtype(other):
19261925
other_mask = isnull(other)
19271926
other = other.astype('i8', copy=False).view('i8')

pandas/tests/frame/test_indexing.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -2522,14 +2522,15 @@ def test_where_dataframe_col_match(self):
25222522
df = DataFrame([[1, 2, 3], [4, 5, 6]])
25232523
cond = DataFrame([[True, False, True], [False, False, True]])
25242524

2525-
out = df.where(cond)
2525+
result = df.where(cond)
25262526
expected = DataFrame([[1.0, np.nan, 3], [np.nan, np.nan, 6]])
2527-
tm.assert_frame_equal(out, expected)
2527+
tm.assert_frame_equal(result, expected)
25282528

2529-
cond.columns = ["a", "b", "c"] # Columns no longer match.
2530-
msg = "Boolean array expected for the condition"
2531-
with tm.assert_raises_regex(ValueError, msg):
2532-
df.where(cond)
2529+
# this *does* align, though has no matching columns
2530+
cond.columns = ["a", "b", "c"]
2531+
result = df.where(cond)
2532+
expected = DataFrame(np.nan, index=df.index, columns=df.columns)
2533+
tm.assert_frame_equal(result, expected)
25332534

25342535
def test_where_ndframe_align(self):
25352536
msg = "Array conditional must be same shape as self"

0 commit comments

Comments
 (0)