0

When I read data from database (or csv, etc.) pandas keep storing date-only data as timestamp and adds 00:00:00? Is there a way to enforce that the column is of type datetime.date and therefore no time is stored?

I tried this but it seems the 00:00:00 is more sticky than originally thought.

pd.to_datetime(mydf['date'], format='%Y-%m-%d').iloc[0]
Out[68]: Timestamp('2015-03-27 00:00:00')

Can i have instead of Timestamp the whole column be of type just datetime.date?

Dnaiel
  • 7,622
  • 23
  • 67
  • 126
  • 2
    The format argument is not doing what you think it is here. – user3483203 Jun 06 '18 at 15:45
  • You can't change the display format for a `datetime64`, you can make a new column of the `str` representation but then it will no longer be a `datetime64` what's the problem here? – EdChum Jun 06 '18 at 15:46
  • @jpp the duplicate does not answer my question, it only answers how to change to str. – Dnaiel Jun 06 '18 at 15:48
  • @EdChum you answer my question but i guess ur answer is basically: " this is not possible" isn't it inneficient use of memmory to store time when it's not needed? or is it just for display purposes? – Dnaiel Jun 06 '18 at 15:49
  • 1
    `mydf['date'].dt.date`? – ayhan Jun 06 '18 at 15:51
  • 1
    No, if you want to store dates, you can just do `mydf['date'] = mydf['date'].dt.date` but then you have `datetime.date` and any arithmetic operations will not be vectorised – EdChum Jun 06 '18 at 15:51
  • @EdChum thanks! so it's actually better to store as timestamp I guess, more efficient for vectorised operations? do you have an example of date-related vectorised operations that would be very slow on dt.date vs pd.Timestamp? – Dnaiel Jun 06 '18 at 15:55
  • @jpp, thanks makes sense then why in that answer datetime.date wasn't considered as an option – Dnaiel Jun 06 '18 at 15:56
  • 1
    well without vectorised support you'd have to iterate row-wise to perform `date` comparisons/arithmetic whilst for datetime64 native dtype you can just do the same thing on the whole columns – EdChum Jun 06 '18 at 15:57
  • 1
    Actually scratch my last comment, I just tried some operations with `datetime.date` and they work fine with comparison and arithmetic operations which is news to me – EdChum Jun 06 '18 at 16:02

0 Answers0