Skip to content

Commit 6d2fc99

Browse files
Merge pull request pandas-dev#11 from jbrockmendel/na
Restore locations of null-handing methods
2 parents 695010c + a32e020 commit 6d2fc99

File tree

6 files changed

+59
-59
lines changed

6 files changed

+59
-59
lines changed

pandas/core/arrays/datetimelike.py

+52-52
Original file line numberDiff line numberDiff line change
@@ -535,58 +535,6 @@ def _maybe_clear_freq(self):
535535
# DatetimeArray and TimedeltaArray
536536
pass
537537

538-
def isna(self):
539-
return self._isnan
540-
541-
@property # NB: override with cache_readonly in immutable subclasses
542-
def _isnan(self):
543-
"""
544-
return if each value is nan
545-
"""
546-
return (self.asi8 == iNaT)
547-
548-
@property # NB: override with cache_readonly in immutable subclasses
549-
def _hasnans(self):
550-
"""
551-
return if I have any nans; enables various perf speedups
552-
"""
553-
return bool(self._isnan.any())
554-
555-
def fillna(self, value=None, method=None, limit=None):
556-
# TODO(GH-20300): remove this
557-
# Just overriding to ensure that we avoid an astype(object).
558-
# Either 20300 or a `_values_for_fillna` would avoid this duplication.
559-
if isinstance(value, ABCSeries):
560-
value = value.array
561-
562-
value, method = validate_fillna_kwargs(value, method)
563-
564-
mask = self.isna()
565-
566-
if is_array_like(value):
567-
if len(value) != len(self):
568-
raise ValueError("Length of 'value' does not match. Got ({}) "
569-
" expected {}".format(len(value), len(self)))
570-
value = value[mask]
571-
572-
if mask.any():
573-
if method is not None:
574-
if method == 'pad':
575-
func = missing.pad_1d
576-
else:
577-
func = missing.backfill_1d
578-
579-
new_values = func(self._data, limit=limit,
580-
mask=mask)
581-
new_values = type(self)(new_values, dtype=self.dtype)
582-
else:
583-
# fill with value
584-
new_values = self.copy()
585-
new_values[mask] = value
586-
else:
587-
new_values = self.copy()
588-
return new_values
589-
590538
def astype(self, dtype, copy=True):
591539
# Some notes on cases we don't have to handle here in the base class:
592540
# 1. PeriodArray.astype handles period -> period
@@ -800,6 +748,23 @@ def map(self, mapper):
800748
# ------------------------------------------------------------------
801749
# Null Handling
802750

751+
def isna(self):
752+
return self._isnan
753+
754+
@property # NB: override with cache_readonly in immutable subclasses
755+
def _isnan(self):
756+
"""
757+
return if each value is nan
758+
"""
759+
return (self.asi8 == iNaT)
760+
761+
@property # NB: override with cache_readonly in immutable subclasses
762+
def _hasnans(self):
763+
"""
764+
return if I have any nans; enables various perf speedups
765+
"""
766+
return bool(self._isnan.any())
767+
803768
def _maybe_mask_results(self, result, fill_value=iNaT, convert=None):
804769
"""
805770
Parameters
@@ -826,6 +791,41 @@ def _maybe_mask_results(self, result, fill_value=iNaT, convert=None):
826791
result[self._isnan] = fill_value
827792
return result
828793

794+
def fillna(self, value=None, method=None, limit=None):
795+
# TODO(GH-20300): remove this
796+
# Just overriding to ensure that we avoid an astype(object).
797+
# Either 20300 or a `_values_for_fillna` would avoid this duplication.
798+
if isinstance(value, ABCSeries):
799+
value = value.array
800+
801+
value, method = validate_fillna_kwargs(value, method)
802+
803+
mask = self.isna()
804+
805+
if is_array_like(value):
806+
if len(value) != len(self):
807+
raise ValueError("Length of 'value' does not match. Got ({}) "
808+
" expected {}".format(len(value), len(self)))
809+
value = value[mask]
810+
811+
if mask.any():
812+
if method is not None:
813+
if method == 'pad':
814+
func = missing.pad_1d
815+
else:
816+
func = missing.backfill_1d
817+
818+
new_values = func(self._data, limit=limit,
819+
mask=mask)
820+
new_values = type(self)(new_values, dtype=self.dtype)
821+
else:
822+
# fill with value
823+
new_values = self.copy()
824+
new_values[mask] = value
825+
else:
826+
new_values = self.copy()
827+
return new_values
828+
829829
# ------------------------------------------------------------------
830830
# Frequency Properties/Methods
831831

pandas/core/arrays/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
259259
# DatetimeLike Interface
260260

261261
def _unbox_scalar(self, value):
262-
if not isinstance(value, (self._scalar_type, type(NaT))):
263-
raise ValueError("'value' should be a a Timestamp.")
262+
if not isinstance(value, self._scalar_type) and value is not NaT:
263+
raise ValueError("'value' should be a Timedelta.")
264264
self._check_compatible_with(value)
265265
return value.value
266266

pandas/core/indexes/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ def astype(self, dtype, copy=True):
345345
dtype = pandas_dtype(dtype)
346346
if is_timedelta64_dtype(dtype) and not is_timedelta64_ns_dtype(dtype):
347347
# Have to repeat the check for 'timedelta64' (not ns) dtype
348-
# so that we can return a numeric index, since pandas will return
349-
# a TimedeltaIndex when dtype='timedelta'
348+
# so that we can return a numeric index, since pandas will return
349+
# a TimedeltaIndex when dtype='timedelta'
350350
result = self._data.astype(dtype, copy=copy)
351351
if self.hasnans:
352352
return Index(result, name=self.name)

pandas/tests/arithmetic/test_datetime64.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ def test_dti_sub_tdi(self, tz_naive_fixture):
19811981
result = dti - tdi
19821982
tm.assert_index_equal(result, expected)
19831983

1984-
msg = 'cannot subtract .*TimedeltaArray'
1984+
msg = 'cannot subtract .*TimedeltaArrayMixin'
19851985
with pytest.raises(TypeError, match=msg):
19861986
tdi - dti
19871987

pandas/tests/indexes/datetimes/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_astype_object_with_nat(self):
177177
def test_astype_raises(self, dtype):
178178
# GH 13149, GH 13209
179179
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
180-
msg = 'Cannot cast DatetimeArray(Mixin)? to dtype'
180+
msg = 'Cannot cast DatetimeArrayMixin to dtype'
181181
with pytest.raises(TypeError, match=msg):
182182
idx.astype(dtype)
183183

pandas/tests/indexes/timedeltas/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_astype_timedelta64(self):
8484
def test_astype_raises(self, dtype):
8585
# GH 13149, GH 13209
8686
idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])
87-
msg = 'Cannot cast TimedeltaArray(Mixin)? to dtype'
87+
msg = 'Cannot cast TimedeltaArrayMixin to dtype'
8888
with pytest.raises(TypeError, match=msg):
8989
idx.astype(dtype)
9090

0 commit comments

Comments
 (0)