[英]How to add numpy datetime list into Pandas Dataframe as column?
I have created a datetime list with 15min interval using this code 我使用此代码创建了间隔为15分钟的日期时间列表
import pandas as pd
import numpy as np
power_data = pd.DataFrame([])
time_data = []
time_data = np.arange('2017-10-31T00:15', '2017-12-01T00:15', dtype='datetime64[15m]'))
the output which I getting is okay as per expected. 按照预期,我得到的输出还可以。 thereafter I try to add this date time array as column into panadas dataframe using this code
此后,我尝试使用此代码将此日期时间数组作为列添加到panadas数据帧中
time_data = pd.Series(time_data)
power_data['Time'] = time_data.values
This code added this Time column correctly but the DateTime value has been changed. 此代码正确添加了此“时间”列,但DateTime值已更改。
0 1973-03-10 16:01:00
1 1973-03-10 16:02:00
2 1973-03-10 16:03:00
.........
2975 1973-03-12 17:36:00
The main culprit is pd.Series(time_data)
which changed the datetime value when it arranging is series. 罪魁祸首是
pd.Series(time_data)
,它在排列为series时更改了datetime值。 My question is how I can add this datetime without changing it's value? 我的问题是如何添加此日期时间而不更改其值?
Have you consider use pd.date_range()
instead? 您是否考虑改用
pd.date_range()
?
This works for me: 这对我有用:
power_data = pd.DataFrame([])
power_data["Time"] = pd.date_range(start="2017-10-31 00:15:00",
end = '2017-12-01 00:15:00',
freq = '15T' )
import pandas as pd
import numpy as np
power_data = pd.DataFrame([])
time_data = []
time_data = np.arange('2017-10-31T00:15', '2017-12-01T00:15', dtype='datetime64')
time_data
I have just removed the [15m]. 我刚刚删除了[15m]。 Everything else remains the same.
其他所有内容保持不变。 So:
所以:
time_data = pd.Series(time_data)
power_data['Time'] = time_data.values
power_data
Now the power_data output looks like this: 现在power_data输出看起来像这样:
0 2017-10-31 00:15:00
1 2017-10-31 00:16:00
2 2017-10-31 00:17:00
3 2017-10-31 00:18:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.