Python 将范围转换为时间戳(单位:秒)

Python 将范围转换为时间戳(单位:秒),python,pandas,Python,Pandas,我在熊猫数据框中有一列,以10为单位从0到17280000。我想把它转换成指定日期的日期戳,从那天午夜开始 那么,假设 time = np.arange(0,172800000, 10) 我想将其转换为以下格式: YYYY-MM-DD: HH:MM:SS.XXX 开始日期应为2016-09-20 以下是我所做的: # Create a dummy frame as an example: test = pd.DataFrame() time = np.arange(0, 172800000

我在熊猫数据框中有一列,以10为单位从0到17280000。我想把它转换成指定日期的日期戳,从那天午夜开始

那么,假设

time = np.arange(0,172800000, 10)
我想将其转换为以下格式:

YYYY-MM-DD: HH:MM:SS.XXX
开始日期应为2016-09-20

以下是我所做的:

# Create a dummy frame as an example: 
test = pd.DataFrame()
time = np.arange(0, 172800000, 10)
test['TIME'] = time
data = np.random.randint(0, 1000, size=len(time))
test['DATA'] = data

# Convert time to datetime:
test['TIME'] = pd.to_datetime(test['TIME'], unit='ms') 
如果我检查数据帧的头部,我会得到以下结果:

             TIME           DATA
0   1970-01-01 00:00:00.000 681
1   1970-01-01 00:00:00.010 986
2   1970-01-01 00:00:00.020 957
3   1970-01-01 00:00:00.030 422
4   1970-01-01 00:00:00.040 319
如何从2016年、09年、20年而不是1970年开始计算年、月和日?

试试:

test['TIME'] = pd.to_datetime('2016-09-20') + pd.to_timedelta(time, 'ms')

这是熊猫的理由。日期范围():

输出:

时间
0  2016-09-20 00:00:00.000
1  2016-09-20 00:00:00.010
2  2016-09-20 00:00:00.020
3  2016-09-20 00:00:00.030
4  2016-09-20 00:00:00.040
5  2016-09-20 00:00:00.050
6  2016-09-20 00:00:00.060
7  2016-09-20 00:00:00.070
8  2016-09-20 00:00:00.080
9  2016-09-20 00:00:00.090
10 2016-09-20 00:00:00.100
11 2016-09-20 00:00:00.110
12 2016-09-20 00:00:00.120
13 2016-09-20 00:00:00.130
14 2016-09-20 00:00:00.140
15 2016-09-20 00:00:00.150
16 2016-09-20 00:00:00.160
17 2016-09-20 00:00:00.170
18 2016-09-20 00:00:00.180
19 2016-09-20 00:00:00.190

(用
periods=20
代替
periods=17280000

我也喜欢这个。谢谢。我喜欢这个,因为它非常简洁!我实际上有10天的数据,以毫秒为单位。因此,创建一个数据帧,然后添加我的原始数据帧可能会占用太多内存,并减慢进程。
import pandas as pd

test = pd.DataFrame({'TIME': pd.date_range(start='2016-09-20',
                                           freq='10ms', periods=20)})
print(test)