Skip to content
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
3 changes: 3 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,9 @@ def convert(
attempt to cast any object types to better types return a copy of
the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
"""
if self.dtype != object:
return [self]
Copy link
Member

Choose a reason for hiding this comment

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

i think this may need to make a copy in some cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you sure?

ser = Series(["a"], dtype="object")
result = ser.infer_objects()
result.iloc[0] = "c"
print(ser)

this updates ser as well, same as the bytes version. If we add a copy, the bytes version would not update anymore

Copy link
Member

Choose a reason for hiding this comment

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

  1. infer_objects in principle makes a copy in cases where it doesn't do inference. 2) I don't think that's what most users (or our internal usages) actually want. 3) ATM the ObjectBlock case that doesn't do inference doesn't make a copy, which is inconsistent with other dtypes. 4) i think adding a copy keyword makes sense xref ENH/PERF: copy=True keyword in methods where copy=False can improve perf #48141, but adding copy keywords in general got pushback recently

Copy link
Member

Choose a reason for hiding this comment

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

I guess we can ignore this since were not consistent about it anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Can address in follow up


values = self.values
if values.ndim == 2:
# maybe_split ensures we only get here with values.shape[0] == 1,
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_infer_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ def test_infer_objects_series(self):

assert actual.dtype == "object"
tm.assert_series_equal(actual, expected)

def test_infer_objects_bytes(self):
# GH#49650
ser = Series([b"a"], dtype="bytes")
expected = ser.copy()
result = ser.infer_objects()
tm.assert_series_equal(result, expected)