Skip to content

Commit 2633863

Browse files
committed
Pattern Series.combine_first on DataFrame.combine_first
DataFrame.combine_first was fixed in pandas-dev#13970. Fix Series using same logic. Remove common._where_compat since it was only referred to by Series.combine_first, name was confusing, and not necessary to the new solution.
1 parent 9525131 commit 2633863

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

pandas/core/common.py

-13
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,6 @@ def _apply_if_callable(maybe_callable, obj, **kwargs):
410410
return maybe_callable
411411

412412

413-
def _where_compat(mask, arr1, arr2):
414-
if arr1.dtype == _NS_DTYPE and arr2.dtype == _NS_DTYPE:
415-
new_vals = np.where(mask, arr1.view('i8'), arr2.view('i8'))
416-
return new_vals.view(_NS_DTYPE)
417-
418-
if arr1.dtype == _NS_DTYPE:
419-
arr1 = tslib.ints_to_pydatetime(arr1.view('i8'))
420-
if arr2.dtype == _NS_DTYPE:
421-
arr2 = tslib.ints_to_pydatetime(arr2.view('i8'))
422-
423-
return np.where(mask, arr1, arr2)
424-
425-
426413
def _dict_compat(d):
427414
"""
428415
Helper function to convert datetimelike-keyed dicts to Timestamp-keyed dict

pandas/core/series.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
is_hashable,
3030
is_iterator,
3131
is_dict_like,
32+
is_dtype_equal,
3233
is_scalar,
3334
_is_unorderable_exception,
3435
_ensure_platform_int,
35-
pandas_dtype)
36+
pandas_dtype,
37+
needs_i8_conversion)
3638
from pandas.core.dtypes.generic import (
3739
ABCSparseArray, ABCDataFrame, ABCIndexClass)
3840
from pandas.core.dtypes.cast import (
41+
find_common_type, maybe_downcast_to_dtype,
3942
maybe_upcast, infer_dtype_from_scalar,
4043
maybe_convert_platform,
4144
maybe_cast_to_datetime, maybe_castable,
@@ -2304,13 +2307,25 @@ def combine_first(self, other):
23042307
other = other.reindex(new_index, copy=False)
23052308
# TODO: do we need name?
23062309
name = ops.get_op_result_name(self, other) # noqa
2307-
rs_vals = com._where_compat(isna(this), other._values, this._values)
2308-
result = self._constructor(rs_vals, index=new_index).__finalize__(self)
2309-
# TODO DK can we simplify this using internals rather than public
2310-
# astype
2311-
if is_datetime64tz_dtype(self.dtype):
2312-
result = result.astype(self.dtype)
2313-
return result
2310+
if not is_dtype_equal(this.dtype, other.dtype):
2311+
new_dtype = find_common_type([this.dtype, other.dtype])
2312+
if not is_dtype_equal(this.dtype, new_dtype):
2313+
this = this.astype(new_dtype)
2314+
if not is_dtype_equal(other.dtype, new_dtype):
2315+
other = other.astype(new_dtype)
2316+
2317+
if needs_i8_conversion(this.dtype):
2318+
mask = isna(this)
2319+
this_values = this.values.view('i8')
2320+
other_values = other.values.view('i8')
2321+
else:
2322+
this_values = this.values
2323+
other_values = other.values
2324+
mask = isna(this_values)
2325+
2326+
rs_vals = np.where(mask, other_values, this_values)
2327+
rs_vals = maybe_downcast_to_dtype(rs_vals, this.dtype)
2328+
return self._constructor(rs_vals, index=new_index).__finalize__(self)
23142329

23152330
def update(self, other):
23162331
"""

0 commit comments

Comments
 (0)