1

I have a dataframe like the following:

df:

ts_dt                        Data                                                       
2019-01-01 01:51:31.540       5
2019-01-01 03:51:31.540      15
2019-01-02 03:51:31.540      15
2019-01-03 03:51:31.540      25
2019-01-03 05:51:31.540      25

I would like to get a list of the distinct days of ts_dt such as

days = [2019-01-01, 2019-01-02, 2019-01-03]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
emax
  • 6,965
  • 19
  • 74
  • 141
  • I'm hedging my bets here with the list of duplicates, but the 3rd one is specifically about columns with the pandas `Timestamp` type. There is also https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.date.html – Martijn Pieters Jan 07 '20 at 12:55
  • So, if `ts_dt` has dtype `Timestamp`, then just use `df.ts_dt.dt.date()`. Then add `.as_type(str)` and `.unique()` and `.tolist()` as required. – Martijn Pieters Jan 07 '20 at 12:55
  • `df['ts_dt'].dt.date.drop_duplicates().tolist()`, but first `pd.to_datetime(df['ts_dt'])` – Erfan Jan 07 '20 at 12:57
  • Or: `df['ts_dt'].dt.date.astype(str).unique()` – Erfan Jan 07 '20 at 12:59

0 Answers0