Search code examples
pythonpandasdatedatetimecalculated-columns

How to convert date column in dataframe from numbers to formatted text? (Python)


I have a column dt["startDateEST"] that ouputs as:

0    2021-07-27 21:46:00-04:00
1    2021-07-27 21:46:00-04:00
2    2021-07-27 19:06:00-04:00
3    2021-07-27 19:06:00-04:00
4    2021-07-27 19:11:00-04:00
                ...           
79   2021-07-27 19:11:00-04:00
80   2021-07-27 19:11:00-04:00
81   2021-07-27 19:11:00-04:00
82   2021-07-27 19:11:00-04:00
83   2021-07-27 19:06:00-04:00
Name: startDateEST, Length: 84, dtype: datetime64[ns, America/New_York]

I'm trying to create a new column dt["startDateEST_Long"] that is dt["startDateEST"] converted to word format as:

0    Jul 27, 2021, 9:46 PM
1    Jul 27, 2021, 9:46 PM
2    Jul 27, 2021, 7:06 PM
3    Jul 27, 2021, 7:06 PM
4    Jul 27, 2021, 7:11 PM
             etc...

I've tried different versions of datetime functions and converting to a string object (.tolist)/parsing through but can't anything to work or run without a Series/attribute error. Please help, thank you!!


Solution

  • First create some sample data:

    dates = pd.date_range("2021-07-27", periods=48, freq="H").tz_localize("America/New_York")
    

    Then fill the DataFrame

    dt = pd.DataFrame({"startDateEST": dates})
    

    Have a look at python datetime formatting strings and format accordingly

    dt["startDateEST_Long"] = dt["startDateEST"].dt.strftime('%b %d, %Y, %I:%M%p')
    
    dt.head()
    
    0 2021-07-27 00:00:00-04:00  Jul 27, 2021, 12:00AM
    1 2021-07-27 01:00:00-04:00  Jul 27, 2021, 01:00AM
    2 2021-07-27 02:00:00-04:00  Jul 27, 2021, 02:00AM
    3 2021-07-27 03:00:00-04:00  Jul 27, 2021, 03:00AM
    4 2021-07-27 04:00:00-04:00  Jul 27, 2021, 04:00AM