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

PythonDateDatetimeFormattingPandas

Python Problem Overview


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

I looked this up and found a related article:

https://stackoverflow.com/questions/5023788/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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

If you want a string mm/dd/yyyy instead of the datetime object, you can use strftime (string format time):

>>> dt.datetime.today().strftime("%m/%d/%Y")
                   # ^ note parentheses
'02/12/2014'

Solution 3 - Python

Using pandas: pd.Timestamp("today").strftime("%m/%d/%Y")

Solution 4 - Python

> pd.datetime.now().strftime("%d/%m/%Y")

this will give output as '11/02/2019'

you can use add time if you want

> pd.datetime.now().strftime("%d/%m/%Y %I:%M:%S")

this will give output as '11/02/2019 11:08:26'

strftime formats

Solution 5 - Python

You can also look into pandas.Timestamp, which includes methods like .now and .today. Unlike pandas.to_datetime('now'), pandas.Timestamp.now() won't default to UTC:

  import pandas as pd

  pd.Timestamp.now() # will return California time
  # Timestamp('2018-12-19 09:17:07.693648')

  pd.to_datetime('now') # will return UTC time
  # Timestamp('2018-12-19 17:17:08')

Solution 6 - Python

i got the same problem so tried so many things but finally this is the solution.

import time 

print (time.strftime("%d/%m/%Y"))

Solution 7 - Python

Easy solution in Python3+:

import time


todaysdate = time.strftime("%d/%m/%Y")

#with '.' isntead of '/'
todaysdate = time.strftime("%d.%m.%Y")

Solution 8 - Python

import datetime
def today_date():
    '''
    utils:
    get the datetime of today
    '''
    date=datetime.datetime.now().date()
    date=pd.to_datetime(date)
    return date
Df['Date'] = today_date()

this could be safely used in pandas dataframes.

Solution 9 - Python

simply just use pd.Timestamp.now()

for example:

input: pd.Timestamp.now()

output: Timestamp('2022-01-12 14:43:05.521896')

I know all you want is Timestamp('2022-01-12') you don't anything after thus we could use replace to remove hour, minutes , second and microsecond here:

input: pd.Timestamp.now().replace(hour=0, minute=0, second=0, microsecond=0)

output: Timestamp('2022-01-12 00:00:00')

but looks too complicated right, here is a simple way use normalize

input: pd.Timestamp.now().normalize()

output: Timestamp('2022-01-12 00:00:00')

Solution 10 - Python

There are already quite a few good answers, but to answer the more general question about "any" period:

Use the function for time periods in pandas. For Day, use 'D', for month 'M' etc.:

>pd.Timestamp.now().to_period('D')
Period('2021-03-26', 'D')

>p = pd.Timestamp.now().to_period('D')
>p.to_timestamp().strftime("%Y-%m-%d")
'2021-03-26'

note: If you need to consider UTC, you can use: pd.Timestamp.utcnow().tz_localize(None).to_period('D')...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexisView Question on Stackoverflow
Solution 1 - PythondirkjotView Answer on Stackoverflow
Solution 2 - PythonjonrsharpeView Answer on Stackoverflow
Solution 3 - PythonCole DiamondView Answer on Stackoverflow
Solution 4 - Pythonsukesh kamathView Answer on Stackoverflow
Solution 5 - PythonJared WilberView Answer on Stackoverflow
Solution 6 - PythonashokdhudlaView Answer on Stackoverflow
Solution 7 - PythonGhostaliView Answer on Stackoverflow
Solution 8 - PythonQijun LiuView Answer on Stackoverflow
Solution 9 - Python吴思位View Answer on Stackoverflow
Solution 10 - PythonntgView Answer on Stackoverflow