Skip to content

Commit

Permalink
Merge branch 'pandas-dev:main' into example
Browse files Browse the repository at this point in the history
  • Loading branch information
Uvi-12 authored Dec 3, 2024
2 parents dfeaec4 + 844b319 commit ec0ffc2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
6 changes: 0 additions & 6 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Period.freq GL08" \
-i "pandas.Period.ordinal GL08" \
-i "pandas.RangeIndex.from_range PR01,SA01" \
-i "pandas.Series.dt.unit GL08" \
-i "pandas.Series.pad PR01,SA01" \
-i "pandas.Timedelta.max PR02" \
-i "pandas.Timedelta.min PR02" \
-i "pandas.Timedelta.resolution PR02" \
Expand All @@ -88,19 +86,15 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.arrays.TimedeltaArray PR07,SA01" \
-i "pandas.core.groupby.DataFrameGroupBy.boxplot PR07,RT03,SA01" \
-i "pandas.core.groupby.DataFrameGroupBy.get_group RT03,SA01" \
-i "pandas.core.groupby.DataFrameGroupBy.nunique SA01" \
-i "pandas.core.groupby.DataFrameGroupBy.plot PR02" \
-i "pandas.core.groupby.DataFrameGroupBy.sem SA01" \
-i "pandas.core.groupby.SeriesGroupBy.get_group RT03,SA01" \
-i "pandas.core.groupby.SeriesGroupBy.plot PR02" \
-i "pandas.core.groupby.SeriesGroupBy.sem SA01" \
-i "pandas.core.resample.Resampler.get_group RT03,SA01" \
-i "pandas.core.resample.Resampler.max PR01,RT03,SA01" \
-i "pandas.core.resample.Resampler.mean SA01" \
-i "pandas.core.resample.Resampler.min PR01,RT03,SA01" \
-i "pandas.core.resample.Resampler.prod SA01" \
-i "pandas.core.resample.Resampler.quantile PR01,PR07" \
-i "pandas.core.resample.Resampler.sem SA01" \
-i "pandas.core.resample.Resampler.std SA01" \
-i "pandas.core.resample.Resampler.transform PR01,RT03,SA01" \
-i "pandas.core.resample.Resampler.var SA01" \
Expand Down
24 changes: 23 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2073,7 +2073,29 @@ def _creso(self) -> int:

@cache_readonly
def unit(self) -> str:
# e.g. "ns", "us", "ms"
"""
The precision unit of the datetime data.
Returns the precision unit for the dtype.
It means the smallest time frame that can be stored within this dtype.
Returns
-------
str
Unit string representation (e.g. "ns").
See Also
--------
TimelikeOps.as_unit : Converts to a specific unit.
Examples
--------
>>> idx = pd.DatetimeIndex(["2020-01-02 01:02:03.004005006"])
>>> idx.unit
'ns'
>>> idx.as_unit("s").unit
's'
"""
# error: Argument 1 to "dtype_to_unit" has incompatible type
# "ExtensionDtype"; expected "Union[DatetimeTZDtype, dtype[Any]]"
return dtype_to_unit(self.dtype) # type: ignore[arg-type]
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@

if TYPE_CHECKING:
from collections.abc import (
Collection,
Sequence,
Sized,
)

from pandas._typing import (
Expand Down Expand Up @@ -1581,7 +1581,7 @@ def _maybe_box_and_unbox_datetimelike(value: Scalar, dtype: DtypeObj):
return _maybe_unbox_datetimelike(value, dtype)


def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
def construct_1d_object_array_from_listlike(values: Collection) -> np.ndarray:
"""
Transform any list-like object in a 1-dimensional numpy array of object
dtype.
Expand All @@ -1599,11 +1599,9 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
-------
1-dimensional numpy array of dtype object
"""
# numpy will try to interpret nested lists as further dimensions, hence
# making a 1D array that contains list-likes is a bit tricky:
result = np.empty(len(values), dtype="object")
result[:] = values
return result
# numpy will try to interpret nested lists as further dimensions in np.array(),
# hence explicitly making a 1D array using np.fromiter
return np.fromiter(values, dtype="object", count=len(values))


def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.ndarray:
Expand Down
48 changes: 30 additions & 18 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ def idxmin(self, skipna: bool = True) -> Series:
Returns
-------
Index
Label of the minimum value.
Series
Indexes of minima in each group.
Raises
------
Expand Down Expand Up @@ -1374,8 +1374,8 @@ def idxmax(self, skipna: bool = True) -> Series:
Returns
-------
Index
Label of the maximum value.
Series
Indexes of maxima in each group.
Raises
------
Expand Down Expand Up @@ -2453,6 +2453,10 @@ def nunique(self, dropna: bool = True) -> DataFrame:
nunique: DataFrame
Counts of unique elements in each position.
See Also
--------
DataFrame.nunique : Count number of distinct elements in specified axis.
Examples
--------
>>> df = pd.DataFrame(
Expand Down Expand Up @@ -2508,8 +2512,8 @@ def idxmax(
Returns
-------
Series
Indexes of maxima in each group.
DataFrame
Indexes of maxima in each column according to the group.
Raises
------
Expand All @@ -2519,6 +2523,7 @@ def idxmax(
See Also
--------
Series.idxmax : Return index of the maximum element.
DataFrame.idxmax : Indexes of maxima along the specified axis.
Notes
-----
Expand All @@ -2532,6 +2537,7 @@ def idxmax(
... {
... "consumption": [10.51, 103.11, 55.48],
... "co2_emissions": [37.2, 19.66, 1712],
... "food_type": ["meat", "plant", "meat"],
... },
... index=["Pork", "Wheat Products", "Beef"],
... )
Expand All @@ -2542,12 +2548,14 @@ def idxmax(
Wheat Products 103.11 19.66
Beef 55.48 1712.00
By default, it returns the index for the maximum value in each column.
By default, it returns the index for the maximum value in each column
according to the group.
>>> df.idxmax()
consumption Wheat Products
co2_emissions Beef
dtype: object
>>> df.groupby("food_type").idxmax()
consumption co2_emissions
food_type
animal Beef Beef
plant Wheat Products Wheat Products
"""
return self._idxmax_idxmin("idxmax", numeric_only=numeric_only, skipna=skipna)

Expand All @@ -2570,8 +2578,8 @@ def idxmin(
Returns
-------
Series
Indexes of minima in each group.
DataFrame
Indexes of minima in each column according to the group.
Raises
------
Expand All @@ -2581,6 +2589,7 @@ def idxmin(
See Also
--------
Series.idxmin : Return index of the minimum element.
DataFrame.idxmin : Indexes of minima along the specified axis.
Notes
-----
Expand All @@ -2594,6 +2603,7 @@ def idxmin(
... {
... "consumption": [10.51, 103.11, 55.48],
... "co2_emissions": [37.2, 19.66, 1712],
... "food_type": ["meat", "plant", "meat"],
... },
... index=["Pork", "Wheat Products", "Beef"],
... )
Expand All @@ -2604,12 +2614,14 @@ def idxmin(
Wheat Products 103.11 19.66
Beef 55.48 1712.00
By default, it returns the index for the minimum value in each column.
By default, it returns the index for the minimum value in each column
according to the group.
>>> df.idxmin()
consumption Pork
co2_emissions Wheat Products
dtype: object
>>> df.groupby("food_type").idxmin()
consumption co2_emissions
food_type
animal Pork Pork
plant Wheat Products Wheat Products
"""
return self._idxmax_idxmin("idxmin", numeric_only=numeric_only, skipna=skipna)

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2658,6 +2658,11 @@ def sem(self, ddof: int = 1, numeric_only: bool = False) -> NDFrameT:
Series or DataFrame
Standard error of the mean of values within each group.
See Also
--------
DataFrame.sem : Return unbiased standard error of the mean over requested axis.
Series.sem : Return unbiased standard error of the mean over requested axis.
Examples
--------
For SeriesGroupBy:
Expand Down

0 comments on commit ec0ffc2

Please sign in to comment.