简体   繁体   English

从开始日期和时间开始的python / pandas,创建日期时间索引

[英]python/pandas from start date + time, create datetime index

Currently I have a dataframe that looks like the following: 目前,我有一个如下数据框:

df = 
     Open    High     Low   Close  TotalVolume
0  113.40  113.54  113.40  113.54         7237
1  113.54  113.58  113.52  113.57        10099
2  113.59  113.81  113.52  113.78        13827
3  113.76  113.94  113.75  113.92        16129
4  113.91  114.01  113.88  113.97        27052
5  113.97  114.11  113.92  114.01        24925
6  114.00  114.15  113.99  114.04        13461
7  114.06  114.14  113.94  113.94        10702
8  113.92  113.99  113.86  113.99         5538
9  113.96  113.96  113.85  113.86        14000

It does not necessarily have to be a datetime index but I felt like it would be the easiest. 它不一定必须是日期时间索引,但我觉得这将是最简单的。 From this I have a variable startDate that follows this format startDate = "03-20-2018t14:00" 从这个我有一个变量startDate遵循这种格式startDate = "03-20-2018t14:00"

From this, this is minute data and for it to run another program, the format has to follow this, but this is the end result I am hoping for: 由此,这是微小的数据,并且要运行其他程序,格式必须遵循此规则,但这是我希望得到的最终结果:

updated_df =
Date          Time     Open    High     Low   Close  TotalVolume
03/20/2018   14:00   113.40  113.54  113.40  113.54         7237
03/20/2018   14:01   113.54  113.58  113.52  113.57        10099
03/20/2018   14:02   113.59  113.81  113.52  113.78        13827
03/20/2018   14:03   113.76  113.94  113.75  113.92        16129
03/20/2018   14:04   113.91  114.01  113.88  113.97        27052
03/20/2018   14:05   113.97  114.11  113.92  114.01        24925
03/20/2018   14:06   114.00  114.15  113.99  114.04        13461
03/20/2018   14:07   114.06  114.14  113.94  113.94        10702
03/20/2018   14:08   113.92  113.99  113.86  113.99         5538
03/20/2018   14:09   113.96  113.96  113.85  113.86        14000

You need to use pandas.date_range() with start , periods and freq parameters. 您需要将pandas.date_range()startperiodsfreq参数一起使用。

df['datetime'] = pd.date_range(start='03-20-2018t14:00', periods=len(df), freq="1min")

Or if you want them separate you can extract date and time from the DatetimeIndex as below: 或者,如果希望它们分开,则可以从DatetimeIndex提取datetime ,如下所示:

datetime_col = pd.date_range(start='03-20-2018t14:00', periods=len(df), freq="1min")
df['Date'] = datetime_col.date
df['Time'] = datetime_col.time

Refer to docs for detailed information. 请参阅文档以获取详细信息。

Output: 输出:

         Date      Time    Open    High     Low   Close  TotalVolume
0  2018-03-20  14:00:00  113.40  113.54  113.40  113.54         7237
1  2018-03-20  14:01:00  113.54  113.58  113.52  113.57        10099
2  2018-03-20  14:02:00  113.59  113.81  113.52  113.78        13827
3  2018-03-20  14:03:00  113.76  113.94  113.75  113.92        16129
4  2018-03-20  14:04:00  113.91  114.01  113.88  113.97        27052
5  2018-03-20  14:05:00  113.97  114.11  113.92  114.01        24925
6  2018-03-20  14:06:00  114.00  114.15  113.99  114.04        13461
7  2018-03-20  14:07:00  114.06  114.14  113.94  113.94        10702
8  2018-03-20  14:08:00  113.92  113.99  113.86  113.99         5538
9  2018-03-20  14:09:00  113.96  113.96  113.85  113.86        14000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM