Skip to content

Commit 8e2627d

Browse files
committed
Merge branch 'master' of https://github.com/pandas-dev/pandas into fuse_kh
2 parents 2beb398 + af1585d commit 8e2627d

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

pandas/_libs/tslibs/period.pyx

+23-12
Original file line numberDiff line numberDiff line change
@@ -1709,14 +1709,14 @@ cdef class _Period:
17091709

17101710
def asfreq(self, freq, how='E'):
17111711
"""
1712-
Convert Period to desired frequency, either at the start or end of the
1713-
interval.
1712+
Convert Period to desired frequency, at the start or end of the interval.
17141713
17151714
Parameters
17161715
----------
1717-
freq : string
1716+
freq : str
1717+
The desired frequency.
17181718
how : {'E', 'S', 'end', 'start'}, default 'end'
1719-
Start or end of the timespan
1719+
Start or end of the timespan.
17201720
17211721
Returns
17221722
-------
@@ -1776,17 +1776,19 @@ cdef class _Period:
17761776

17771777
def to_timestamp(self, freq=None, how='start', tz=None):
17781778
"""
1779-
Return the Timestamp representation of the Period at the target
1780-
frequency at the specified end (how) of the Period.
1779+
Return the Timestamp representation of the Period.
1780+
1781+
Uses the target frequency specified at the part of the period specified
1782+
by `how`, which is either `Start` or `Finish`.
17811783
17821784
Parameters
17831785
----------
1784-
freq : string or DateOffset
1786+
freq : str or DateOffset
17851787
Target frequency. Default is 'D' if self.freq is week or
1786-
longer and 'S' otherwise
1788+
longer and 'S' otherwise.
17871789
how : str, default 'S' (start)
1788-
'S', 'E'. Can be aliased as case insensitive
1789-
'Start', 'Finish', 'Begin', 'End'
1790+
One of 'S', 'E'. Can be aliased as case insensitive
1791+
'Start', 'Finish', 'Begin', 'End'.
17901792
17911793
Returns
17921794
-------
@@ -2385,16 +2387,25 @@ class Period(_Period):
23852387
Parameters
23862388
----------
23872389
value : Period or str, default None
2388-
The time period represented (e.g., '4Q2005')
2390+
The time period represented (e.g., '4Q2005').
23892391
freq : str, default None
2390-
One of pandas period strings or corresponding objects
2392+
One of pandas period strings or corresponding objects.
2393+
ordinal : int, default None
2394+
The period offset from the gregorian proleptic epoch.
23912395
year : int, default None
2396+
Year value of the period.
23922397
month : int, default 1
2398+
Month value of the period.
23932399
quarter : int, default None
2400+
Quarter value of the period.
23942401
day : int, default 1
2402+
Day value of the period.
23952403
hour : int, default 0
2404+
Hour value of the period.
23962405
minute : int, default 0
2406+
Minute value of the period.
23972407
second : int, default 0
2408+
Second value of the period.
23982409
"""
23992410

24002411
def __new__(cls, value=None, freq=None, ordinal=None,

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class PeriodDtype(PandasExtensionDtype):
793793
Parameters
794794
----------
795795
freq : str or DateOffset
796-
The frequency of this PeriodDtype
796+
The frequency of this PeriodDtype.
797797
798798
Attributes
799799
----------

pandas/core/groupby/ops.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,13 @@ def agg_series(self, obj, func):
672672
pass
673673
else:
674674
raise
675-
return self._aggregate_series_pure_python(obj, func)
675+
except TypeError as err:
676+
if "ndarray" in str(err):
677+
# raised in libreduction if obj's values is no ndarray
678+
pass
679+
else:
680+
raise
681+
return self._aggregate_series_pure_python(obj, func)
676682

677683
def _aggregate_series_fast(self, obj, func):
678684
func = self._is_builtin_func(func)

pandas/tests/extension/decimal/test_decimal.py

+52
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,55 @@ def test_array_ufunc_series_defer():
426426

427427
tm.assert_series_equal(r1, expected)
428428
tm.assert_series_equal(r2, expected)
429+
430+
431+
def test_groupby_agg():
432+
# Ensure that the result of agg is inferred to be decimal dtype
433+
# https://github.com/pandas-dev/pandas/issues/29141
434+
435+
data = make_data()[:5]
436+
df = pd.DataFrame(
437+
{"id1": [0, 0, 0, 1, 1], "id2": [0, 1, 0, 1, 1], "decimals": DecimalArray(data)}
438+
)
439+
440+
# single key, selected column
441+
expected = pd.Series(to_decimal([data[0], data[3]]))
442+
result = df.groupby("id1")["decimals"].agg(lambda x: x.iloc[0])
443+
tm.assert_series_equal(result, expected, check_names=False)
444+
result = df["decimals"].groupby(df["id1"]).agg(lambda x: x.iloc[0])
445+
tm.assert_series_equal(result, expected, check_names=False)
446+
447+
# multiple keys, selected column
448+
expected = pd.Series(
449+
to_decimal([data[0], data[1], data[3]]),
450+
index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 1)]),
451+
)
452+
result = df.groupby(["id1", "id2"])["decimals"].agg(lambda x: x.iloc[0])
453+
tm.assert_series_equal(result, expected, check_names=False)
454+
result = df["decimals"].groupby([df["id1"], df["id2"]]).agg(lambda x: x.iloc[0])
455+
tm.assert_series_equal(result, expected, check_names=False)
456+
457+
# multiple columns
458+
expected = pd.DataFrame({"id2": [0, 1], "decimals": to_decimal([data[0], data[3]])})
459+
result = df.groupby("id1").agg(lambda x: x.iloc[0])
460+
tm.assert_frame_equal(result, expected, check_names=False)
461+
462+
463+
def test_groupby_agg_ea_method(monkeypatch):
464+
# Ensure that the result of agg is inferred to be decimal dtype
465+
# https://github.com/pandas-dev/pandas/issues/29141
466+
467+
def DecimalArray__my_sum(self):
468+
return np.sum(np.array(self))
469+
470+
monkeypatch.setattr(DecimalArray, "my_sum", DecimalArray__my_sum, raising=False)
471+
472+
data = make_data()[:5]
473+
df = pd.DataFrame({"id": [0, 0, 0, 1, 1], "decimals": DecimalArray(data)})
474+
expected = pd.Series(to_decimal([data[0] + data[1] + data[2], data[3] + data[4]]))
475+
476+
result = df.groupby("id")["decimals"].agg(lambda x: x.values.my_sum())
477+
tm.assert_series_equal(result, expected, check_names=False)
478+
s = pd.Series(DecimalArray(data))
479+
result = s.groupby(np.array([0, 0, 0, 1, 1])).agg(lambda x: x.values.my_sum())
480+
tm.assert_series_equal(result, expected, check_names=False)

0 commit comments

Comments
 (0)