簡體   English   中英

Python Dataframe 從幾百萬行的大日期時間索引中提取唯一日期列表

[英]Python Dataframe extract list of unique dates from a big datetimeindex of few million rows

我的數據框有大約 1700 萬行。 索引是日期時間。 它是大約一秒分辨率的一年數據。 現在我想從中提取一個唯一日期列表。

我的代碼:

# sample df

df.index = DatetimeIndex(['2019-10-01 05:00:00', '2019-10-01 05:00:01',
               '2019-10-01 05:00:05', '2019-10-01 05:00:06',
               '2019-10-01 05:00:08', '2019-10-01 05:00:09',
               '2019-10-01 05:00:12', '2019-10-01 05:00:13',
               '2019-10-01 05:00:15', '2019-10-01 05:00:17',
               ...
               '2020-11-14 19:59:21', '2020-11-14 19:59:23',
               '2020-11-14 19:59:31', '2020-11-14 19:59:32',
               '2020-11-14 19:59:37', '2020-11-14 19:59:38',
               '2020-11-14 19:59:45', '2020-11-14 19:59:46',
               '2020-11-14 19:59:55', '2020-11-14 19:59:56'],
              dtype='datetime64[ns]', name='timestamp', length=17796121, freq=None)
dates = df.index.strftime('&Y-&m-%d').unique()

我上面的代碼給出了輸出。 但大約花了五分鍾。 有沒有更好的方法可以讓我更快地獲得日期?

保存stftime以備您真正需要這些字符串時使用。 這很慢。

嘗試這個:

dates = np.unique(dates.date)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM