简体   繁体   English

Pandas 数据帧值从日期时间更改为时间戳

[英]Pandas dataframe value changes from datetime to Timestamp

I created a datetime variable and when appended into DataFrame, it automatically converted to Timestamp.我创建了一个日期时间变量,当附加到 DataFrame 时,它​​会自动转换为时间戳。 how can this happen?这怎么会发生? anyone can some idea on this?任何人都可以对此有所了解吗?

mydatetime=datetime.datetime(2019,1,22)
df_test=pd.DataFrame(columns=['datetimecol','value'])
df_test.loc[len(df_test)]=[mydatetime,12.3]
df_test
df_test.datetimecol[0]

This is by design.这是设计使然。 Pandas specializes in vectorised operations facilitated by NumPy arrays. Pandas 专门研究由 NumPy 数组促进的矢量化操作。 1 Hence Pandas can store a datetime series in a contiguous memory block backed by integer ( int64 ) values. 1因此 Pandas 可以将datetime序列存储在由整数 ( int64 ) 值支持的连续内存块中。 In contrast, a list of datetime.datetime values will consist of a sequence of pointers to a number of different memory addresses.相比之下, datetime.datetime值的列表将由指向许多不同内存地址的指针序列组成。

Apart from better memory management, pd.Timestamp supports the functionality available to datetime.datetime objects and, in addition, useful Pandas-specific functionality.除了更好的内存管理之外, pd.Timestamp支持datetime.datetime对象可用的功能,此外,还有有用的 Pandas 特定功能。

In general, avoid import datetime if you can when using Pandas.通常,在使用 Pandas 时,如果可以,请避免import datetime Not only is it usually unnecessary, but it will also likely be less efficient than working directly with pd.Timestamp objects.它不仅是通常是不必要的,但它也可能会比直接与工作效率较低pd.Timestamp对象。


1 See this answer for more information on how precisely this works. 1请参阅此答案以了解有关其工作方式的更多信息。

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

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