Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

However, this didn't particularly answer my question.

I used the suggested:

dt.date.today 

But after

import datetime as dt      date = dt.date.today print date  <built-in method today of type object at 0x000000001E2658B0>   Df['Date'] = date 

I didn't get what I actually wanted which as a clean date format of today's date...in Month/Day/Year.

How can I create a variable of today's day in order for me to input that variable in a DataFrame?

like image 779
Alexis Avatar asked Feb 12 '14 20:02

Alexis


People also ask

How do you assign the current date to a variable in Python?

today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

How do I get today's date in dd-mm-yyyy in Python?

Use strftime() function of a datetime class For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.


1 Answers

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.

like image 146
dirkjot Avatar answered Sep 21 '22 07:09

dirkjot