1

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.

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

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.