user357269
user357269

Reputation: 1913

pandas group by but keep original index and duplicate aggregate values

I have an integer indexed pd.Series with dtype datetime64[ns] called timestamps. (timestamps is sorted but this shouldn't matter)

I would like to calculate another series called first_on_day with the same index and the same dtype that represents the earliest time on the day.

The following code works:

first_on_day = timestamps.groupby(timestamps.dt.date).apply(lambda series: pd.Series(series.min(), index=series.index))

Is there a more idiomatic way to doing this?

Related: Pandas aggregate values by years but keep original TimeSeries index

Upvotes: 0

Views: 442

Answers (1)

not_speshal
not_speshal

Reputation: 23146

IIUC, you want:

timestamps.groupby(timestamps.dt.date).transform("min")

Upvotes: 1

Related Questions