3

I have a dataframe with dates in string format. I convert those dates to timestamp, so that I could use this date column in the later part of the code. Everything is fine with calculations/comparisons etc, but I would like the timestamp to appear in %d.%m.%Y format, as opposed to default %Y-%m-%d. Let me illustrate it -

dt=pd.DataFrame({'date':['09.12.1998','07.04.2014']},index=[1,2])
dt
Out[4]: 
         date
1  09.12.1998
2  07.04.2014

dt['date_1']=pd.to_datetime(dt['date'],format='%d.%m.%Y')

dt
Out[7]: 
         date     date_1
1  09.12.1998 1998-12-09
2  07.04.2014 2014-04-07

I would like to have dt['date_1'] to de displayed in the same format as dt['date']. I don't wish to use .strftime() function because it will convert the datatype from timestamp to string.

In Nutshell: How can I invoke the python system in displaying the timestamp in the format of my choice(months could be like APR, MAY etc), rather than getting a default format(like 1998-12-09), keeping in mind that the data type remains a timestamp, rather than string?

6
  • If you explicitly don't want to use strftime maybe you have to write your own method to display the date in custom format, but it will have to be a string. Commented Aug 1, 2017 at 9:36
  • I'm not sure if I really understand your specific requirement, but wouldn't it be possible to do print(datetime.datetime.fromtimestamp("yourtimestamp").strftime('%d.%m.%Y'))? This will just display the timestamp as a date. Commented Aug 1, 2017 at 9:39
  • Well, that's exactly I do not want. I don't want to disturb the dataype. So, was just wondering if there exists an implicit method to prespecify the display format. Commented Aug 1, 2017 at 9:40
  • 1
    @OliverS but the object would stay the same. Technically speaking I think that python would create a new object with the strings in it (which is what is happening anyway when you print a dataframe) and then displaying that. The underlying dataframe wouldn't be changed. Commented Aug 1, 2017 at 9:43
  • There is no disturbing of the datatype. If you want to print a datetime object in custom format, it has to be a string. Write your own class, specify how the date should be printed in the __str__ method and it will be called when printing the variable. Commented Aug 1, 2017 at 9:43

3 Answers 3

3

It seems Pandas didn't implement this option yet: https://github.com/pandas-dev/pandas/issues/11501

Sign up to request clarification or add additional context in comments.

3 Comments

oh I see. May be that's how it is. Thank you for sharing the link.
I wish I had seen your answer before spending 2 hours on google!
Thanks Stael and Droravr. Much appreciated :)
0

having a look at https://pandas.pydata.org/pandas-docs/stable/options.html looks like you can set the display to achieve some of this, although not all.

display.date_dayfirst When True, prints and parses dates with the day first, eg 20/01/2005

display.date_yearfirst When True, prints and parses dates with the year first, eg 2005/01/20

so you can have dayfirst, but they haven't included names for months.

On a more fundamental level, whenever you're displaying something it is a string, right? I'm not sure why you wouldn't be able to convert it when you're displaying it without having to change the original dataframe.


your code would be:

pd.set_option("display.date_dayfirst", True)

except actually this doesn't work:

https://github.com/pandas-dev/pandas/issues/11501

the options have been implemented for parsing, but not for displaying.

1 Comment

Hi Stael, sorry, it did not work. I tried various permutations to it, but to no avail. dt['date_1']=pd.to_datetime(dt['date'],format='%d.%m.%Y',dayfirst=True)...... I did set your statement as well. But didn't help. Thanks a lot for yourself trying to help :)
0

Hallo Stael/Cezar/Droravr, Thank you all for providing your inputs. I value your time and appreciate your help a lot. Thanks for sharing this link https://github.com/pandas-dev/pandas/issues/11501 as well. I went through the link and understood that this problem can be broken down to a 'displaying problem' ultimately, as also expounded by jreback. This issue to have the dates displayed to your desired format has been marked as an Enhancement, so probably will be added to future versions.

All I wanted was the have to dates exported as dd-mm-yyy and by just formatting the string while exporting, we could solve this problem. So, I sorted this issue by exporting the file as -

dt.to_csv(filename, date_format='%d-%m-%Y',index=False).

    date     date_1
09.12.1998 09-12-1998
07.04.2014 07-04-2014

Thus, this issue stands SOLVED.

Once again, thank you all for your kind help and the precious hours you spent with this issue. Deeply appreciated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.