3

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

6 Comments

You did not cover the datetime format conversion in your answer.1/1/2018 14:00 and please do not using your own data .
@Wen what would be the easiest way to convert the time which is in UTC right now, to EST?
@Wen, edited the answer. However, As the docs have shown, I can pass string as a date, not necessarily the datetime object as startdate. Correct me if I am wrong. Thanks.
@HarvIpan I think this is input '03-20-2018t14:00' not '03-20-2018 14:00'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.