简体   繁体   English

在 Pandas 数据框中格式化时间戳

[英]Formatting Timestamp in Pandas dataframe

I have a large dataset representing an electrical signal (as an array) that need's to be passed as a pandas dataframe to another function.我有一个表示电信号(作为数组)的大型数据集,需要将其作为 Pandas 数据帧传递给另一个函数。 The thing is the dataframe has to have an index with a freq attribute, and the original dataset doesn't have a timestamp, 1d array with the samples, but I know the sample freq (86Hz) so I can assign a timestamp for each sample:问题是数据帧必须有一个带有 freq 属性的索引,而原始数据集没有时间戳,带有样本的一维数组,但我知道样本频率(86Hz)所以我可以为每个样本分配一个时间戳:

>>>a = [1,2,3,4,5,6]
>>>b = []
>>>j=0
>>>for i in a:
>>>    b.append(round(j, 3))
>>>    j = j+(1/86)
>>>c = np.c_[b, a]
     ([[ 0.   ,  1.   ],
       [ 0.012,  2.   ],
       [ 0.023,  3.   ],
       [ 0.035,  4.   ],
       [ 0.047,  5.   ],
       [ 0.058,  6.   ]])

Then I turn it in to a dataframe:然后我把它变成一个数据框:

d = pd.DataFrame(data=c[0:,1], index=c[0:,0])

        0
0.000   1.0
0.012   2.0
0.023   3.0
0.035   4.0
0.047   5.0
0.058   6.0

The problem is the index has no freq attribute, I think it's a formatting issue, but not sure, and after a loot of googling I didn't find anything.问题是索引没有 freq 属性,我认为这是一个格式问题,但不确定,经过大量的谷歌搜索后我没有找到任何东西。

When I write:当我写:

d.index.freq

it should return 86, but gives me an:它应该返回 86,但给了我一个:

AttributeError: 'Float64Index' object has no attribute 'freq' AttributeError: 'Float64Index' 对象没有属性 'freq'

By the way, the timestamp is in seconds, starting with the first sample... or that is my intent.顺便说一句,时间戳以秒为单位,从第一个样本开始......或者这就是我的意图。

If I understand correctly you can do it like this:如果我理解正确,你可以这样做:

In [109]: d.index = pd.timedelta_range(d.index.min(), periods=len(d), freq='86L')

In [110]: d
Out[110]:
                   0
00:00:00         1.0
00:00:00.086000  2.0
00:00:00.172000  3.0
00:00:00.258000  4.0
00:00:00.344000  5.0
00:00:00.430000  6.0

In [111]: d.index.freq
Out[111]: <86 * Millis>

In [112]: d.index.dtype
Out[112]: dtype('<m8[ns]')

In [113]: d.index.dtype_str
Out[113]: 'timedelta64[ns]'

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

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