|
18 | 18 | import pandas as pd
|
19 | 19 | from pandas import (
|
20 | 20 | Categorical,
|
| 21 | + CategoricalDtype, |
21 | 22 | CategoricalIndex,
|
22 | 23 | DataFrame,
|
23 | 24 | DatetimeIndex,
|
@@ -1820,6 +1821,54 @@ def test_loc_getitem_sorted_index_level_with_duplicates(self):
|
1820 | 1821 | result = df.loc[("foo", "bar")]
|
1821 | 1822 | tm.assert_frame_equal(result, expected)
|
1822 | 1823 |
|
| 1824 | + def test_additional_element_to_categorical_series_loc(self): |
| 1825 | + # GH#47677 |
| 1826 | + result = Series(["a", "b", "c"], dtype="category") |
| 1827 | + result.loc[3] = 0 |
| 1828 | + expected = Series(["a", "b", "c", 0], dtype="object") |
| 1829 | + tm.assert_series_equal(result, expected) |
| 1830 | + |
| 1831 | + def test_additional_categorical_element_loc(self): |
| 1832 | + # GH#47677 |
| 1833 | + result = Series(["a", "b", "c"], dtype="category") |
| 1834 | + result.loc[3] = "a" |
| 1835 | + expected = Series(["a", "b", "c", "a"], dtype="category") |
| 1836 | + tm.assert_series_equal(result, expected) |
| 1837 | + |
| 1838 | + def test_loc_set_nan_in_categorical_series(self, any_numeric_ea_dtype): |
| 1839 | + # GH#47677 |
| 1840 | + srs = Series( |
| 1841 | + [1, 2, 3], |
| 1842 | + dtype=CategoricalDtype(Index([1, 2, 3], dtype=any_numeric_ea_dtype)), |
| 1843 | + ) |
| 1844 | + # enlarge |
| 1845 | + srs.loc[3] = np.nan |
| 1846 | + expected = Series( |
| 1847 | + [1, 2, 3, np.nan], |
| 1848 | + dtype=CategoricalDtype(Index([1, 2, 3], dtype=any_numeric_ea_dtype)), |
| 1849 | + ) |
| 1850 | + tm.assert_series_equal(srs, expected) |
| 1851 | + # set into |
| 1852 | + srs.loc[1] = np.nan |
| 1853 | + expected = Series( |
| 1854 | + [1, np.nan, 3, np.nan], |
| 1855 | + dtype=CategoricalDtype(Index([1, 2, 3], dtype=any_numeric_ea_dtype)), |
| 1856 | + ) |
| 1857 | + tm.assert_series_equal(srs, expected) |
| 1858 | + |
| 1859 | + @pytest.mark.parametrize("na", (np.nan, pd.NA, None, pd.NaT)) |
| 1860 | + def test_loc_consistency_series_enlarge_set_into(self, na): |
| 1861 | + # GH#47677 |
| 1862 | + srs_enlarge = Series(["a", "b", "c"], dtype="category") |
| 1863 | + srs_enlarge.loc[3] = na |
| 1864 | + |
| 1865 | + srs_setinto = Series(["a", "b", "c", "a"], dtype="category") |
| 1866 | + srs_setinto.loc[3] = na |
| 1867 | + |
| 1868 | + tm.assert_series_equal(srs_enlarge, srs_setinto) |
| 1869 | + expected = Series(["a", "b", "c", na], dtype="category") |
| 1870 | + tm.assert_series_equal(srs_enlarge, expected) |
| 1871 | + |
1823 | 1872 | def test_loc_getitem_preserves_index_level_category_dtype(self):
|
1824 | 1873 | # GH#15166
|
1825 | 1874 | df = DataFrame(
|
|
0 commit comments