Reputation: 139
I have a dataframe in pandas having two fields:
UsageStartDate UsageEndDate
5/1/2020 12:00:00 AM 5/1/2020 11:59:59 PM
I am trying to convert them to 24-hour format.
I am using the following:
df['UsageStartDate'] = pd.to_datetime(df['UsageStartDate'], format='%m/%d/%Y %I:%M:%S %p')
df['UsageEndDate'] = pd.to_datetime(df['UsageEndDate'], format='%m/%d/%Y %I:%M:%S %p')
The UsageEndDate
field gives proper output: 2020-05-01 23:59:59
However, the UsageStartDate
gives: 2020-05-01
I also tried to parse the date during reading of the csv file:
df = pd.read_csv('./data/sample.csv', parse_dates=['UsageStartDate', 'UsageEndDate'])
It returned the same result as above.
Kindly help.
Upvotes: 1
Views: 61
Reputation: 2049
Pandas will display fields without trailing zeros, as an aesthetic choice. Your actual Timestamp value is correct:
df['UsageStartDate'] + pd.Timedelta(seconds=1)
0 2020-05-01 00:00:01
Name: UsageStartDate, dtype: datetime64[ns]
If you want the full value as a string, you can get it with the Timestamp string formatter, strftime():
df['UsageStartDate'].dt.strftime('%m/%d/%Y %I:%M:%S %p')
0 05/01/2020 12:00:00 AM
Name: UsageStartDate, dtype: object # <- Note that this is now object type
Upvotes: 1