Change time format in python dataframe

There is a difference between

  • the content of a dataframe cell (a binary value) and
  • its presentation (displaying it) for us, humans.

So the question is: How to reach the appropriate presentation of my datas without changing the data / data types themselves?

Here is the answer:

  • If you use the Jupyter notebook for displaying your dataframe, or
  • if you want to reach a presentation in the form of an HTML file (even with many prepared superfluous id and class attributes for further CSS styling — you may or you may not use them),

use styling. Styling don't change data / data types of columns of your dataframe.

Now I show you how to reach it in the Jupyter notebook — for a presentation in the form of HTML file see the note near the end of this answer.

I will suppose that your column DOB already has the datetime64 type (you have shown that you know how to reach it). I prepared a simple dataframe (with only one column) to show you some basic styling:

  • Not styled:

    df
    
          DOB
0  2019-07-03
1  2019-08-03
2  2019-09-03
3  2019-10-03
  • Styling it as mm/dd/yyyy:

    df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})
    
          DOB
0  07/03/2019
1  08/03/2019
2  09/03/2019
3  10/03/2019
  • Styling it as dd-mm-yyyy:

    df.style.format({"DOB": lambda t: t.strftime("%d-%m-%Y")}) 
    
          DOB
0  03-07-2019
1  03-08-2019
2  03-09-2019
3  03-10-2019

Be careful!
The returning object is NOT a dataframe — it is an object of the class Styler, so don't assign it back to df:

Don't do this:

df = df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})    # Don't do this!

(Every dataframe has its Styler object accessible by its .style property, and we changed this df.style object, not the dataframe itself.)


Questions and Answers:

  • Q: Why your Styler object (or an expression returning it) used as the last command in a Jupyter notebook cell displays your (styled) table, and not the Styler object itself?

  • A: Because every Styler object has a callback method ._repr_html_() which returns an HTML code for rendering your dataframe (as a nice HTML table).

    Jupyter Notebook IDE calls this method automatically to render objects which have it.


Note:

You don't need the Jupyter notebook for styling (i.e., for nice outputting a dataframe without changing its data / data types).

A Styler object has a method render(), too, if you want to obtain a string with the HTML code (e.g., for publishing your formatted dataframe on the Web, or simply present your table in the HTML format):

df_styler = df.style.format({"DOB": lambda t: t.strftime("%m/%d/%Y")})
HTML_string = df_styler.render()

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: Pandas

    The date-time default format is “YYYY-MM-DD”. Hence, December 8, 2020, in the date format will be presented as “2020-12-08”. The datetime format can be changed and by changing we mean changing the sequence and style of the format. 

    Function used 

    strftime() can change the date format in python. 

    Syntax:

    strftime(format) 

    Where, format is a string representing the type of required date format. 

    • For year %y 
    • For month %m
    • For day %d 

    Approach

    • Import module
    • Provide date
    • Change the format using above function

    Example

    Python3

    import pandas as pd

    date_sr = pd.Series(pd.date_range(

        '2019-12-31', periods=3, freq='M', tz='Asia/Calcutta'))

    ind = ['Day 1', 'Day 2', 'Day 3']

    date_sr.index = ind

    change_format = date_sr.dt.strftime('%d,%m,%Y')

    print(change_format)

    Output

    Change time format in python dataframe

    Example

    Python3

    import pandas as pd

    date_sr = pd.to_datetime(pd.Series("2020-12-08"))

    change_format = date_sr.dt.strftime('%d/%m/%Y')

    print(change_format)

    Output

    Change time format in python dataframe

    Example

    Python3

    import pandas as pd

    date_sr = pd.to_datetime(pd.Series("2012-09-02"))

    change_format = date_sr.dt.strftime('%d-%m-%Y')

    print(change_format)

    Output

    Change time format in python dataframe


    How do I change the format of a Dataframe time?

    Use DataFrame. style. format() and Lambda Function to Change datetime Format.

    How do I change the format of a time in Python?

    Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

    How do I change the date format from YYYY MM DD in pandas?

    Pandas – Change Format of Date Column.
    # change the format to DD-MM-YYYY. df['Col'] = df['Col'].dt. ... .
    # covert to datetime. df['Birthday'] = pd. ... .
    # date in MM-DD-YYYY format. df['Birthday2'] = df['Birthday'].dt. ... .
    # date in DD-MM-YYYY format. df['Birthday3'] = df['Birthday'].dt. ... .
    # date in Month day, Year format..

    How can I format date in pandas?

    import pandas as pd. # input in mm.dd.yyyy format. date = ['01.02.2019'] ... .
    import pandas as pd. # date (mm.dd.yyyy) and time (H:MM:SS) date = ['01.02.2019 1:30:00 PM'] ... .
    import pandas as pd. # pandas interprets this date to be in m-d-yyyy format. print(pd. ... .
    import pandas as pd. date = '2019-07-31 12:00:00-UTC' print(pd..