Home > database >  How do I format pandas timestamp or day_name() function to DD/MM/YYYY?
How do I format pandas timestamp or day_name() function to DD/MM/YYYY?

Time:06-10

I'm new to python and pandas, so thank you all for helping me in advance.

I lived in the UK, so I use the DD/MM/YYYY format, and I've checked, the region for my PC is also in the format of DD/MM/YYYY in the system.

However when I wrote the following:

def mock_day_of_week():
    # return Monday
    temp = pd.Timestamp("15/11/2021")
    print(temp.day_name())

mock_day_of_week()


def mock_day_of_week1():
    # return Monday too
    temp = pd.Timestamp("11/15/2021")
    print(temp.day_name())

mock_day_of_week1()

Regardless if it's DD/MM/YYYY or MM/DD/YYYY, they both return Monday, so that's great. With the exception that the DD/MM/YYYY also return this:

sys:1: UserWarning: Parsing '15/11/2021' in DD/MM/YYYY format. Provide format or specify infer_datetime_format=True for consistent parsing.

However, when I test the following functions:

def mock_day_of_week3():
    # should return Sunday for 12th June 2022, but return as Tuesday for 6th December 2022 instead
    temp = pd.Timestamp("12/6/2022")
    print(temp.day_name())

mock_day_of_week3()


def mock_day_of_week4():
    # should return Friday for 6th May 2022 but return as Sunday for 5th June 2022 instead
    temp = pd.Timestamp("06/05/2022")
    print(temp.day_name())

mock_day_of_week4()

The Timestamp function start reading as MM/DD/YYYY instead.

What is the correct format that I need to add to the argument so that it force the Timestamp to recognise DD/MM/YYYY format instead.

I've tried the suggested:

temp = pd.Timestamp("06/05/2022", infer_datetime_format=True)
print(temp.day_name())

but this gives an error of:

File "pandas_libs\tslibs\timestamps.pyx", line 1248, in pandas._libs.tslibs.timestamps.Timestamp.new TypeError: new() got an unexpected keyword argument 'infer_datetime_format'

I'm stump, thanks in advance for any input on how to resolve this issue.

CodePudding user response:

I believe the simplest way is to specify them as integers via named arguments:

>>> pd.Timestamp(day=12, month=6, year=2022)

You can also use dayfirst from pd.to_datetime to remove ambiguity on whether month or days are first:

>>> pd.to_datetime('12/6/2022', dayfirst=True)

Finally, you may also provide a format:

pd.to_datetime('12/6/2022', format='%d/%m/%Y')

CodePudding user response:

add tz parameter with your timezone

temp = pd.Timestamp("05/06/2022", tz='Europe/London')

CodePudding user response:

I believe your question is:

What is the correct format that I need to add to the argument so that it force the Timestamp to recognise DD/MM/YYYY format?

One way to be very explicit about this is the following:

    temp = pd.to_datetime("12/6/2021", format="%d/%m/%Y")
    print(temp.month_name())

Alternatively, you can to this:

    temp = pd.to_datetime("12/6/2021", dayfirst=True)
    print(temp.month_name())
  • Related