Remove Seconds From Datetime in Python

This article focuses on how to remove seconds on Python datetime objects. The seconds can be removed in two ways – simply truncating seconds by turning them to 0 or by rounding off seconds into minutes.

In this post, we assume you want to work with datetime objects, string datetime formats, or pandas dataframes/series (the assumption is sufficient in most cases).

Figure 1: Stripping seconds in Python datetime object.

Remove Seconds from datetime: Using <datetime>.replace()

The datetime object supports replacing arguments with values of our choice. The attributes provided by datetime.datetime and can be replaced include year, month, day, hour, minute, second, microsecond, and tzinfo (time zone information).

If we want to strip the second in datetime, we simply set the second argument to zero. Here are some examples.

Output:

2022-08-11 16:07:29.593771 <-- datetime now - date, time
2022-08-11 16:07:00.593771 <-- second removed
2022-08-11 16:07:00 <-- second and microsecond removed

In the above code, we were able to set seconds and/or microseconds to zero and, therefore, effectively remove it/them from the datetime object.

Suppose, instead of simply truncating, we round off the datetime value to the nearest minute – if seconds<30, we set it to zero, otherwise, set it to zero and increase minutes by 1.

Output:

time2 --> 2021-03-29 14:26:30
rounded time2 --> 2021-03-29 14:27:00
time2 --> 2022-08-11 16:32:20.940362
rounded time2 --> 2022-08-11 16:32:00
time3 --> 2022-06-12 08:07:38.005679
rounded time3 --> 2022-06-12 08:08:00
time4 --> 2022-11-28 12:12:21.005679
rounded time4 --> 2022-11-28 12:12:00

The function minute_round() sets seconds and microseconds to zero, retains hour as is, and timedelta rounds off seconds into the nearest minute.

Strip Seconds in datetime String

When dealing with datetime on string format, you must parse it before using replace to remove seconds. Parsing a string into datetime is achieved using <str>.strptime(<obj>, <format>) function.

Output:

2022-06-30 12:06:00

The code snippet above does two things – change the string str_dt1 into a datetime object using strptime() and then, as before, use replace() to set seconds and microseconds to zero.

If you want to round off seconds, convert the string to datetime and proceed as before by calling the minute_round() function.

Sometimes strptime() function fails to parse some strings to datetime. If you find this limitation problematic based on your task, you can use the dateparser package instead (you might have to install the library before using it).

The package contains the parse() function, which can parse strings more efficiently than strptime(). Here are some examples.

Output:

Now: 2022-08-11 17:01:16.805044
2 days ago at 11:00:06
2022-08-09 11:00:06
2022-01-10 10:37:00

The function parse() returns a datetime object. Once we have that, we can remove seconds as we did before.

How to Remove Seconds in Pandas Data

Python pandas is a popular library used for data manipulation and analysis. This section covers removing seconds in pandas datetime objects on dataframes or series. We will save the following data as “example.csv” and use it in the examples that follow.

Dates1 Dates2
2022-09-09 10:10:12 Jan 12, 2021 at 10am
2019-08-16 13:01:57 2022-01-29 14:00:58
1987/12/31 11:59:58 In 10 days
Output:
before parsing:
Dates1		object
Dates2		object
dtype: object

In the above code, we loaded example.csv and checked the data types for the two columns. At this point, the two columns are just objects (strings).

In the next step, we parse the strings into datetime. The inbuilt datetime module in Python can handle the parsing of Dates1 but not Dates2. We will use dateparse.parse() to pass Dates2.

Once the strings are passed to datetime, seconds are removed.

Output (Formated for better viewing):

Now 2022-08-11 17:34:43.712168
after parsing:
Dates1		datetime64[ns]
Dates2		datetime64[ns]
dtype: object
  Dates1 Dates2
0 2022-09-09 10:10:00 2021-01-12 10:00:00
1 2019-08-16 13:01:00 2022-01-29 14:00:00
2 1987-12-31 11:59:00 2022-08-21 17:34:00

Conclusion

In this article, we have seen how to remove seconds in datetime by either truncating it or rounding it off. We also saw how to convert strings or pandas object into datetime (whenever possible, of course).