python中的datetime模块·详解

Ref.:https://www.datacamp.com/tutorial/converting-strings-datetime-objects

A Gentle Introduction to the Python datetime Module

The datetime module, which comes in-built with Python, can be used whenever you need to work with dates, times, or time intervals for any application built using Python. It provides convenient classes and methods for representing and manipulating date and time data.

Let’s understand the main classes under this module, as we’ll be converting them into the datetime objects:

1. datetime.date

This class represents a date (year, month, and day) and provides methods for working with dates, such as calculating the difference between two dates and formatting dates as strings.

Suppose we have a dataset containing the daily stock prices for a company. We can use the date class to extract the dates from the dataset and plot the stock prices over time.

Here’s a snippet showcasing the usage of the date class:

 

from datetime import date

# create a date object representing March 1, 2023

start_date = date(2023, 3, 1)

# extract information such as the year, month, and day

year = start_date.year

month = start_date.month

day = start_date.day

# get the day of the week (Note: Monday is coded as 0, and Sunday as 6)

weekday = start_date.weekday()

# the date can be formatted as a string if needed

date_str = start_date.strftime('%Y-%m-%d')

2. datetime.time

This class represents a time of day (hour, minute, second, and microsecond) and provides methods for working with times, such as comparing times and formatting times as strings.

Let’s say we have a dataset containing the ending time for a race; we can use the time class to extract the hours and minutes of each competitor ending the race.

 

from datetime import time

# create a time object with the microsecond granularity

end_time = time(15, 45, 30, 500000)

# get the hour and minute

hour = end_time.hour

minute = end_time.minute

second = end_time.second

microsecond = end_time.microsecond

3. datetime.datetime

This class represents a date and time and provides methods for working with both. It combines the functionality of the date and time classes.

It is commonly used in data analysis tasks that involve time-series data with a high temporal resolution, such as hourly or minute-level data. Assume we have a dataset containing the hourly electricity demand for a city. We can use the datetime class to extract the date and time from the dataset and plot the electricity demand over time.

 

from datetime import datetime

# create a datetime object representing March 1, 2023 at 9:30 AM

start_datetime = datetime(2023, 3, 1, 9, 30)

# get the year, month, day, hour, and minute

year = start_datetime.year

month = start_datetime.month

day = start_datetime.day hour = start_datetime.hour

minute = start_datetime.minute

4. datetime.timedelta

This class represents a duration or time interval and provides methods for working with time intervals, such as adding or subtracting time intervals from dates or times.

Suppose we have a dataset containing the start and end times of a set of events, and we want to calculate the total duration of all the events. We can use the timedelta class to calculate the duration of each event and sum them up.

 

from datetime import timedelta

# create a timedelta object representing 3 hours and 15 minutes

event_duration = timedelta(hours=3, minutes=15)

# get the total duration in seconds

event_duration_seconds = event_duration.total_seconds()

# add the duration to a start time to get an end time

event_start_time = datetime(2023, 3, 1, 18, 15)

event_end_time = event_start_time + event_duration

In all of these cases we showed, we used datetime objects, but the real-world data often remains as a string in practice. And converting to a datetime object unlocks all of the above functionalities that are powerful in data science analysis and applications.

Convert a String to a datetime Object in Python Using datetime.strptime()

In Python, we can use the datetime.strptime() method to convert a string to a datetime object. The strptime() method takes two arguments: the string to be converted and a format string specifying the input string's format.

The format string uses a combination of formatting codes to represent the various components of the date and time. Here are some of the most commonly used formatting codes:

  • %Y: 4-digit year
  • %y: 2-digit year
  • %m: 2-digit month (01-12)
  • %d: 2-digit day of the month (01-31)
  • %H: 2-digit hour (00-23)
  • %M: 2-digit minute (00-59)
  • %S: 2-digit second (00-59)

Now that we understand the strptime directives, the process of converting strings to datetime objects can be simplified.

  • Step 01: Analyze the date-time string that can be converted for patterns that match the formatting codes.
  • Step 02: Create the date-time format from the strptime()directives.
  • Step 03: Pass the string and format to the function and receive the object as output.

Let’s put these steps into action.

Converting a string in a specific format to a datetime object

 

from datetime import datetime

# Example with the standard date and time format

date_str = '2023-02-28 14:30:00'

date_format = '%Y-%m-%d %H:%M:%S'

date_obj = datetime.strptime(date_str, date_format)

print(date_obj)

# Example with a different format

date_str = '02/28/2023 02:30 PM'

date_format = '%m/%d/%Y %I:%M %p'

date_obj = datetime.strptime(date_str, date_format) print(date_obj)

In the first example, we have a string representing a date and time in the format ‘YYYY-MM-DD HH:MM:SS’, and for the second example in a different format, ‘MM/DD/YYYY HH:MM AM/PM’.

For both cases, after we specify the correct format string as the second argument to strptime() to receive the correct datetime object.

Converting a string with timezone information to a datetime object

 

from datetime import datetime

date_str = '2023-02-28 14:30:00+05:30'

date_format = '%Y-%m-%d %H:%M:%S%z'

date_obj = datetime.strptime(date_str, date_format)

print(date_obj)

In this example, we have a string representing a date and time with timezone information in the format ‘YYYY-MM-DD HH:MM:SS+TZOFFSET’, where TZOFFSET is the timezone offset in hours and minutes from UTC. We specify the format string as the second argument to strptime(), including the %z formatting code to parse the timezone offset. 

While the function we saw above may seem easy in theory, it can also be a source of frustration when things go wrong in practice.

Troubleshooting Common strptime() Errors

Here are some common errors you might encounter and how to fix them:

ValueError: time data 'date_string' does not match format '%Y-%m-%d %H:%M:%S'

The most common error occurs when the input string does not match the format string. Please double-check that the input string and format string match exactly. 

 

import datetime

# When input has two-digit year instead of four-digit year

date_str = '23-03-01'

date_obj = datetime.datetime.strptime(date_str, '%y-%m-%d')

# Raises ValueError: time data '23-03-01' does not match format '%y-%m-%d'

# When the input has missing leading zeros for hour and minute

time_str = '8:30'

time_obj = datetime.datetime.strptime(time_str, '%H:%M')

# Raises ValueError: time data '8:30' does not match format '%H:%M'

TypeError: strptime() argument 1 must be str, not 'int'

The next common error occurs when you pass an integer to datetime.strptime() or time.strptime() instead of a string. Ensure all the values you’re passing to the function are strings.

 

# Example 1: Integer instead of string

date_int = 20230301

date_obj = datetime.datetime.strptime(date_int, '%Y%m%d')

# Raises TypeError: strptime() argument 1 must be str, not int

# Example 2: List instead of string

date_list = [2023, 3, 1]

date_obj = datetime.datetime.strptime(date_list, '%Y-%m-%d')

# Raises TypeError: strptime() argument 1 must be str, not list

ValueError: unconverted data remains: ':00'

This error occurs when leftover characters exist in the input string, which is not matched by the format string. For example, this error will occur if the format string only specifies the year, month, and day, but the input string also contains the time of day. To debug this error, ensure that the format string matches the entire input string.

 

# when input string contains time of day

date_str = '2023-03-01 12:30:00'

date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d')

# Raises ValueError: unconverted data remains: 12:30:00

# When input string contains extra characters

date_str = '2023-03-01T00:00:00Z'

date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d')

# Raises ValueError: unconverted data remains: T00:00:00Z

Conclusion

It’s safe to say that datetime.strptime() method provides a flexible and powerful way to convert strings to datetime objects in Python and can be used to handle a wide range of date and time formats. Why don’t you grab our Dates and Times Cheatsheet for later reference?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值