Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG/PERF: Series.combine_first converting int64 to float64 #51777

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
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
avoid loss of precision
  • Loading branch information
lukemanley committed Mar 4, 2023
commit f60a3d6ad4eeeff2f57a4c627503e4334e257b03
2 changes: 1 addition & 1 deletion 2 doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Groupby/resample/rolling

Reshaping
^^^^^^^^^
- Bug in :meth:`Series.combine_first` converting ``int64`` dtype to ``float64`` (:issue:`51764`)
- Bug in :meth:`Series.combine_first` converting ``int64`` dtype to ``float64`` and losing precision on very large integers (:issue:`51764`)
-

Sparse
Expand Down
24 changes: 13 additions & 11 deletions 24 pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@
from pandas.core.dtypes.cast import (
LossySetitemError,
convert_dtypes,
find_common_type,
maybe_box_native,
maybe_cast_pointwise_result,
)
from pandas.core.dtypes.common import (
is_dict_like,
is_dtype_equal,
is_extension_array_dtype,
is_integer,
is_iterator,
Expand Down Expand Up @@ -3268,18 +3266,22 @@ def combine_first(self, other) -> Series:
falcon NaN
dtype: float64
"""
new_index = self.index.union(other.index)
this = self.reindex(new_index, copy=False)
other = other.reindex(new_index, copy=False)
if this.dtype.kind == "M" and other.dtype.kind != "M":
other = to_datetime(other)
from pandas.core.reshape.concat import concat

combined = this.where(notna(this), other)
new_index = self.index.union(other.index)

if not is_dtype_equal(combined.dtype, self.dtype):
dtype = find_common_type([self.dtype, other.dtype])
combined = combined.astype(dtype, copy=False)
this = self
null_mask = isna(this)
if null_mask.any():
drop = this.index[null_mask].intersection(other.index[notna(other)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a quick comment what this is doing? Took me a while to figure it out, don't want to do this again in 6 months time :)

Otherwise lgtm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've simplified this logic a bit. should be easier to follow now

if len(drop):
this = this.drop(drop)

other = other.reindex(other.index.difference(this.index), copy=False)
if this.dtype.kind == "M" and other.dtype.kind != "M":
other = to_datetime(other)
combined = concat([this, other]).reindex(new_index, copy=False)
combined.name = self.name
return combined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a finalise call with self, otherwise we will loose metadata.

This might changed result ordering? Can we do a reindex in the end?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks


def update(self, other: Series | Sequence | Mapping) -> None:
Expand Down
6 changes: 3 additions & 3 deletions 6 pandas/tests/series/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def test_combine_first_timezone_series_with_empty_series(self):

def test_combine_first_preserves_dtype(self):
# GH51764
s1 = Series([4, 5])
s2 = Series([6, 7, 8])
s1 = Series([np.iinfo(np.int64).min, np.iinfo(np.int64).max])
s2 = Series([1, 2, 3])
result = s1.combine_first(s2)
expected = Series([4, 5, 8])
expected = Series([np.iinfo(np.int64).min, np.iinfo(np.int64).max, 3])
tm.assert_series_equal(result, expected)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.