Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在熊猫系列中添加时间戳?_Python_Pandas - Fatal编程技术网

Python 如何在熊猫系列中添加时间戳?

Python 如何在熊猫系列中添加时间戳?,python,pandas,Python,Pandas,我正在尝试创建带有时间戳字段的Pandas Series对象start: a = pd.Series(index=['preceding_id', 'file', 'start'], dtype=[np.int, np.str, np.datetime64], ) 它失败的原因是 TypeError: data type not understood 有人能解释一下我做错了什么吗? 我一直在寻找pandas中的Date和datetime对象,但文档只说明了如何将其用作索引-这不是我想要的

我正在尝试创建带有时间戳字段的Pandas Series对象
start

a = pd.Series(index=['preceding_id', 'file', 'start'],  dtype=[np.int, np.str, np.datetime64], )
它失败的原因是

TypeError: data type not understood
有人能解释一下我做错了什么吗?
我一直在寻找pandas中的Date和datetime对象,但文档只说明了如何将其用作索引-这不是我想要的


谢谢大家!

一个系列只能有一种数据类型。如果要在一个系列中存储多个类型,系列的类型将是
object
,即通用Python类型

In [12]: Series([1, 'some string', pd.to_datetime('2014-01-01')])
Out[12]: 
0                      1
1            some string
2    2014-01-01 00:00:00
dtype: object
这没问题。组成元素的类型被保留。例如,上面系列中的时间戳仍然是一个时间戳,我们可以通过访问它看到这一点

In [13]: Series([1, 'some string', pd.to_datetime('2014-01-01')])[2]
Out[13]: Timestamp('2014-01-01 00:00:00', tz=None)

总之,不要指定数据类型。通常,在没有您的帮助的情况下,可以正确处理它们。

一个系列只能有一种数据类型。如果要在一个系列中存储多个类型,系列的类型将是
object
,即通用Python类型

In [12]: Series([1, 'some string', pd.to_datetime('2014-01-01')])
Out[12]: 
0                      1
1            some string
2    2014-01-01 00:00:00
dtype: object
这没问题。组成元素的类型被保留。例如,上面系列中的时间戳仍然是一个时间戳,我们可以通过访问它看到这一点

In [13]: Series([1, 'some string', pd.to_datetime('2014-01-01')])[2]
Out[13]: Timestamp('2014-01-01 00:00:00', tz=None)

总之,不要指定数据类型。一般来说,它们在没有您的帮助的情况下会得到正确处理。

您确定不需要数据帧吗

如果是这样的话,它看起来就像:

data = {'preceeding_id': [list of ids],
        'file': [list of files], 
        'start': [list of timestamps]}

df = pd.DataFrame(data)
df.start = pd.to_datetime(df.start)

或者,如果您正在从文件或其他内容中读取数据,则可以轻松地对Panda的大多数I/O函数使用
parse_dates=True
。事实上,Pandas在指定正确的数据类型方面非常出色。

您确定不需要数据帧吗

如果是这样的话,它看起来就像:

data = {'preceeding_id': [list of ids],
        'file': [list of files], 
        'start': [list of timestamps]}

df = pd.DataFrame(data)
df.start = pd.to_datetime(df.start)

或者,如果您正在从文件或其他内容中读取数据,则可以轻松地对Panda的大多数I/O函数使用
parse_dates=True
。实际上,Pandas在分配正确的数据类型方面非常出色。

是的,最终我想要一个数据帧。我考虑使用电子表格/数据框之类的东西,然后在给定事件上添加行/系列。因为我没能让一行正常工作,所以我没有尝试数据帧。是的,最终我想要一个数据帧。我考虑使用电子表格/数据框之类的东西,然后在给定事件上添加行/系列。因为我没有设法让一行正常工作,所以我没有尝试数据帧。