Skip to content

Commit 20e5a8c

Browse files
committed
Add documentation and annotations
2 parents 99994aa + 73b5578 commit 20e5a8c

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

doc/source/user_guide/style.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
"\n",
352352
"- Using [.set_table_styles()][table] to control broader areas of the table with specified internal CSS. Although table styles allow the flexibility to add CSS selectors and properties controlling all individual parts of the table, they are unwieldy for individual cell specifications. Also, note that table styles cannot be exported to Excel. \n",
353353
"- Using [.set_td_classes()][td_class] to directly link either external CSS classes to your data cells or link the internal CSS classes created by [.set_table_styles()][table]. See [here](#Setting-Classes-and-Linking-to-External-CSS). These cannot be used on column header rows or indexes, and also won't export to Excel. \n",
354-
"- Using the [.apply()][apply] and [.map()][map] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes; [.apply_index()][applyindex] and [.map_index()][mapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n",
354+
"- Using the [.apply()][apply] and [.map()][map] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes: [.apply_index()][applyindex] and [.map_index()][mapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n",
355355
"\n",
356356
"[table]: ../reference/api/pandas.io.formats.style.Styler.set_table_styles.rst\n",
357357
"[styler]: ../reference/api/pandas.io.formats.style.Styler.rst\n",

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ Sparse
625625

626626
ExtensionArray
627627
^^^^^^^^^^^^^^
628+
- Bug in :class:`NumpyExtensionArray` where error was not raised if value cannot be inserted without changing the dtype (:issue:`51044`)
628629
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
629630
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
630631
- Bug in various :class:`DataFrame` reductions for pyarrow temporal dtypes returning incorrect dtype when result was null (:issue:`59234`)

pandas/core/arrays/numpy_.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import (
44
TYPE_CHECKING,
5+
Any,
56
Literal,
67
)
78

@@ -240,7 +241,7 @@ def _values_for_factorize(self) -> tuple[np.ndarray, float | None]:
240241
fv = np.nan
241242
return self._ndarray, fv
242243

243-
def _validate_setitem_value(self, value):
244+
def _validate_setitem_value(self, value: Any) -> Any | None:
244245
"""
245246
Check if we have a scalar that we can cast losslessly.
246247
@@ -252,7 +253,11 @@ def _validate_setitem_value(self, value):
252253
if type(value) == self.dtype.type:
253254
return value
254255

255-
if isinstance(value, NumpyExtensionArray) and value.dtype == self.dtype:
256+
if (
257+
isinstance(value, NumpyExtensionArray)
258+
or isinstance(value, np.ndarray)
259+
or isinstance(value, pd.Series)
260+
) and value.dtype == self.dtype:
256261
return value
257262

258263
if (
@@ -275,7 +280,7 @@ def _validate_setitem_value(self, value):
275280
or (isinstance(value, str) and self.dtype.kind in "US")
276281
or (self.dtype.kind == "O")
277282
) and not isna(value):
278-
if self.dtype.type(value) == value: # -> Problem
283+
if self.dtype.type(value) == value:
279284
return value
280285

281286
if isna(value):

pandas/core/series.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,14 +1815,30 @@ def _set_name(
18151815
Parrot 30.0
18161816
Parrot 20.0
18171817
Name: Max Speed, dtype: float64
1818+
1819+
We can pass a list of values to group the Series data by custom labels:
1820+
18181821
>>> ser.groupby(["a", "b", "a", "b"]).mean()
18191822
a 210.0
18201823
b 185.0
18211824
Name: Max Speed, dtype: float64
1825+
1826+
Grouping by numeric labels yields similar results:
1827+
1828+
>>> ser.groupby([0, 1, 0, 1]).mean()
1829+
0 210.0
1830+
1 185.0
1831+
Name: Max Speed, dtype: float64
1832+
1833+
We can group by a level of the index:
1834+
18221835
>>> ser.groupby(level=0).mean()
18231836
Falcon 370.0
18241837
Parrot 25.0
18251838
Name: Max Speed, dtype: float64
1839+
1840+
We can group by a condition applied to the Series values:
1841+
18261842
>>> ser.groupby(ser > 100).mean()
18271843
Max Speed
18281844
False 25.0
@@ -1845,11 +1861,16 @@ def _set_name(
18451861
Parrot Captive 30.0
18461862
Wild 20.0
18471863
Name: Max Speed, dtype: float64
1864+
18481865
>>> ser.groupby(level=0).mean()
18491866
Animal
18501867
Falcon 370.0
18511868
Parrot 25.0
18521869
Name: Max Speed, dtype: float64
1870+
1871+
We can also group by the 'Type' level of the hierarchical index
1872+
to get the mean speed for each type:
1873+
18531874
>>> ser.groupby(level="Type").mean()
18541875
Type
18551876
Captive 210.0
@@ -1865,12 +1886,17 @@ def _set_name(
18651886
b 3
18661887
dtype: int64
18671888
1889+
To include `NA` values in the group keys, set `dropna=False`:
1890+
18681891
>>> ser.groupby(level=0, dropna=False).sum()
18691892
a 3
18701893
b 3
18711894
NaN 3
18721895
dtype: int64
18731896
1897+
We can also group by a custom list with NaN values to handle
1898+
missing group labels:
1899+
18741900
>>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
18751901
>>> ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
18761902
>>> ser.groupby(["a", "b", "a", np.nan]).mean()

0 commit comments

Comments
 (0)