1

I created a datetime variable and when appended into DataFrame, it automatically converted to Timestamp. 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]
jpp
  • 159,742
  • 34
  • 281
  • 339
mojop
  • 41
  • 6

1 Answers1

1

This is by design. Pandas specializes in vectorised operations facilitated by NumPy arrays.1 Hence Pandas can store a datetime series in a contiguous memory block backed by integer (int64) values. In contrast, a list of datetime.datetime values will consist of a sequence of pointers to a number of different memory addresses.

Apart from better memory management, pd.Timestamp supports the functionality available to datetime.datetime objects and, in addition, useful Pandas-specific functionality.

In general, avoid import datetime if you can when using Pandas. Not only is it usually unnecessary, but it will also likely be less efficient than working directly with pd.Timestamp objects.


1 See this answer for more information on how precisely this works.

jpp
  • 159,742
  • 34
  • 281
  • 339