Search code examples
pythonpandasmilliseconds

How to change time format that is already stored in json using python?


I have data stored in a JSON file and am reading it in with Pandas. The format of the time is 'Jun 10, 2021, 01:05:30:565'. I would like to present the time column with its match. However, python gives this error: Unknown string format:', 'Jun 10, 2021, 01:05:30:565 AM').

I used: DS [pd.to_datetime(day + ' ' + time)] = value

That line worked with other columns that have time : HH:MM: SS. but with milliseconds I'm unable to present what I want.


Solution

  • From your example :

    >>> import pandas as pd
    
    >>> df = pd.DataFrame({'date': ['Jun 10, 2021, 01:05:30:565 AM']}, 
    ...                   index = [0]) 
    >>> df
        date
    0   Jun 10, 2021, 01:05:30:565 AM
    

    We can convert the date column to DateTime like so :

    >>> df['date'] = pd.to_datetime(df['date'], format="%b %d, %Y, %H:%M:%S:%f %p")
    >>> df
        date
    0   2021-06-10 01:05:30.565