Python Dataframe 从数百万行的大日期时间索引中提取唯一日期列表

标签 python pandas dataframe datetime datetimeindex

我的数据框有大约 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/

相关文章:

python - Pandas 合并处理连接输出中的重复项

python - dict 的 dict 到 pandas 数据框

python - 迭代数组并在文件中搜索数组中的每个项目

Python 原始 IPv6 套接字错误

python - 按级别对列进行分组,按其他级别的 pandas 对其他列进行分组

python - 如何在 python 中读取大型 .jl 文件

R:重复 data.frame 的行 k 次并为新行值添加前缀

python - 从正在运行的线程调用其他线程上的协程

python - 使用skimage调整到更大的分辨率会导致keras中的形状类型错误

python-3.x - 根据字段值重现/中断行