我的数据框有大约 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)
关于Python Dataframe 从数百万行的大日期时间索引中提取唯一日期列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64939852/