[英]Python creating a new dataframe with list of unique dates as index
I have dateframe with index as df.index = 我有索引为df.index =的日期框架
2016-08-01 06:45:00
2016-08-01 07:00:00
2016-08-01 07:15:00
.
.
2018-03-28 11:30:00
2018-03-28 11:45:00
2018-03-28 12:00:00
I want to create a new dataframe that it will have only unique dates as 我想创建一个新的数据框,它将只有唯一的日期作为
new_df.index = new_df.index =
2016-08-01
2016-08-02
.
.
2018-03-28
2018-03-29
So, how to create a new dataframe with unique dates as index? 那么,如何创建一个以唯一日期作为索引的新数据框?
There is a build-in collection set in Python, that have only unique elements. 有一个内置的集合设置在Python,仅具有独特的元素。 You can do something like it: 您可以执行类似的操作:
new_data = sorted(list(set(old_data)))
If you want to crop out time in your datetime lines, you can modify this code with generators: 如果要在日期时间行中裁剪时间,可以使用生成器修改此代码:
new_data = sorted(list(set([elem[:10] for elem in old_data])))
Note, that if you have some info linked with your datetimes (like values in dict with datetime keys), you must handle elements removal before doing it. 请注意,如果您有一些与日期时间相关联的信息(例如带日期时间键的dict中的值),则必须先处理元素删除操作。
Since you have not provided any data I will assume it is not important. 由于您没有提供任何数据,因此我认为这并不重要。
Your first DataFrame
appears to have a DatetimeIndex
and it appears you want to convert it to a PeriodIndex
. 您的第一个DataFrame
似乎具有DatetimeIndex
并且您似乎想将其转换为PeriodIndex
。 You can do this to get the unique days with df.resample(rule='D').asfreq()
. 您可以使用df.resample(rule='D').asfreq()
来获得唯一日期。 Docs are here 文件在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.