Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

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
Better name, tests
  • Loading branch information
jbrockmendel committed Jul 27, 2023
commit c62dcaafde69e21e300e2c2ec582f29ec5bf9b8d
2 changes: 1 addition & 1 deletion pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def _can_hold_na(self) -> bool:
"""
return True

def _maybe_promote(self, item: Any) -> tuple[DtypeObj, Any]:
def _find_compatible_dtype(self, item: Any) -> tuple[DtypeObj, Any]:
"""
Find the minimal dtype that we need to upcast to in order to hold
the given item.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def _maybe_promote(dtype: DtypeObj, fill_value=np.nan):
# a cached version.

if not isinstance(dtype, np.dtype):
return dtype._maybe_promote(fill_value)
return dtype._find_compatible_dtype(fill_value)

if not is_scalar(fill_value):
# with object dtype there is nothing to promote, and the user can
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:

return find_common_type(non_cat_dtypes)

def _maybe_promote(self, item) -> tuple[DtypeObj, Any]:
def _find_compatible_dtype(self, item) -> tuple[DtypeObj, Any]:
from pandas.core.dtypes.missing import is_valid_na_for_dtype

if item in self.categories or is_valid_na_for_dtype(
Expand Down Expand Up @@ -1569,7 +1569,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
except (KeyError, NotImplementedError):
return None

def _maybe_promote(self, item) -> tuple[DtypeObj, Any]:
def _find_compatible_dtype(self, item) -> tuple[DtypeObj, Any]:
from pandas.core.dtypes.cast import maybe_promote
from pandas.core.dtypes.missing import is_valid_na_for_dtype

Expand Down Expand Up @@ -2316,7 +2316,7 @@ def __from_arrow__(self, array: pa.Array | pa.ChunkedArray):
arr = array.cast(self.pyarrow_dtype, safe=True)
return array_class(arr)

def _maybe_promote(self, item: Any) -> tuple[DtypeObj, Any]:
def _find_compatible_dtype(self, item: Any) -> tuple[DtypeObj, Any]:
if isinstance(item, pa.Scalar):
Copy link
Member

Choose a reason for hiding this comment

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

what happens if item is null? The pyarrow null

Copy link
Member Author

Choose a reason for hiding this comment

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

IIUC pyarrow nulls are now typed. id prefer to be strict about making these match, but dont care that much. am hoping @jorisvandenbossche will weigh in

if not item.is_valid:
# TODO: ask joris for help making these checks more robust
Copy link
Member Author

Choose a reason for hiding this comment

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

@jorisvandenbossche any thoughts here? (not time-sensitive)

Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,28 @@ def test_loc_drops_level(self):


class TestLocSetitemWithExpansion:
def test_series_loc_setitem_with_expansion_categorical(self):
# NA value that can't be held in integer categories
ser = Series([1, 2, 3], dtype="category")
ser.loc[3] = pd.NaT
assert ser.dtype == object

def test_series_loc_setitem_with_expansion_interval(self):
idx = pd.interval_range(1, 3)
ser2 = Series(idx)
ser2.loc[2] = np.nan
assert ser2.dtype == "interval[float64, right]"

ser2.loc[3] = pd.NaT
assert ser2.dtype == object

def test_series_loc_setitem_with_expansion_list_object(self):
ser3 = Series(range(3))
ser3.loc[3] = []
assert ser3.dtype == object
item = ser3.loc[3]
assert isinstance(item, list) and len(item) == 0

@pytest.mark.slow
def test_loc_setitem_with_expansion_large_dataframe(self):
# GH#10692
Expand Down