How to set a variable to be “Today's” date in Python/Pandas

后端 未结 8 2192
醉话见心
醉话见心 2020-12-23 19:35

I am trying to set a variable to equal today\'s date.

I looked this up and found a related article:

Set today date as default value in the model

Howe

8条回答
  •  囚心锁ツ
    2020-12-23 20:16

    You mention you are using Pandas (in your title). If so, there is no need to use an external library, you can just use to_datetime

    >>> pandas.to_datetime('today').normalize()
    Timestamp('2015-10-14 00:00:00')
    

    This will always return today's date at midnight, irrespective of the actual time, and can be directly used in pandas to do comparisons etc. Pandas always includes 00:00:00 in its datetimes.

    Replacing today with now would give you the date in UTC instead of local time; note that in neither case is the tzinfo (timezone) added.

    In pandas versions prior to 0.23.x, normalize may not have been necessary to remove the non-midnight timestamp.

提交回复
热议问题