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"
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.
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.