Skip to content

ENH: dtype-unaware (empty) objects ("any" dtype) #48110

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

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,8 @@ def _setitem_with_indexer_missing(self, indexer, value):
curr_dtype = self.obj.dtype
curr_dtype = getattr(curr_dtype, "numpy_dtype", curr_dtype)
new_dtype = maybe_promote(curr_dtype, value)[0]
elif self.obj.empty and is_object_dtype(self.obj.dtype):
new_dtype = self.obj.dtype
else:
new_dtype = None

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ def test_loc_setitem_empty_series(self):
# GH#5226

# partially set with an empty object series
ser = Series(dtype=object)
ser = Series()
ser.loc[1] = 1
tm.assert_series_equal(ser, Series([1], index=[1]))
ser.loc[3] = 3
Expand All @@ -1949,7 +1949,7 @@ def test_loc_setitem_empty_series_float(self):
# GH#5226

# partially set with an empty object series
ser = Series(dtype=object)
ser = Series()
ser.loc[1] = 1.0
tm.assert_series_equal(ser, Series([1.0], index=[1]))
ser.loc[3] = 3.0
Expand All @@ -1959,7 +1959,7 @@ def test_loc_setitem_empty_series_str_idx(self):
# GH#5226

# partially set with an empty object series
ser = Series(dtype=object)
ser = Series()
ser.loc["foo"] = 1
tm.assert_series_equal(ser, Series([1], index=["foo"]))
ser.loc["bar"] = 3
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/indexing/test_set_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_series_set_value():
dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
index = DatetimeIndex(dates)

s = Series(dtype=object)
s = Series()
s._set_value(dates[0], 1.0)
s._set_value(dates[1], np.nan)

Expand Down
19 changes: 17 additions & 2 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,14 @@ class TestSetitemWithExpansion:
def test_setitem_empty_series(self):
# GH#10193
key = Timestamp("2012-01-01")
series = Series(dtype=object)
series = Series()
series[key] = 47
expected = Series(47, [key])
tm.assert_series_equal(series, expected)

def test_setitem_empty_series_datetimeindex_preserves_freq(self):
# GH#33573 our index should retain its freq
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
series = Series([], DatetimeIndex([], freq="D"))
key = Timestamp("2012-01-01")
series[key] = 47
expected = Series(47, DatetimeIndex([key], freq="D"))
Expand Down Expand Up @@ -1660,3 +1660,18 @@ def test_setitem_empty_mask_dont_upcast_dt64():
ser.mask(mask, "foo", inplace=True)
assert ser.dtype == dti.dtype # no-op -> dont upcast
tm.assert_series_equal(ser, orig)


def test_setitem_on_series_dtype_object():
# GH#19647
result = Series(dtype="object")
result.loc["int"] = 1
result.loc["float"] = 2.0
expected = Series(data=[1, 2.0], index=["int", "float"]).astype("object")
tm.assert_series_equal(result, expected)

result = Series()
result.loc["int"] = 1
result.loc["float"] = 2.0
expected = Series(data=[1, 2.0], index=["int", "float"]).astype("float")
tm.assert_series_equal(result, expected)