Summary of date processing in Python (datetime and dateutil)

Hello, this is An'nin. When I was taking the "Working with Dates and Times in Python" course at DataCamp, there were a lot of unfamiliar date processing (I used only pd.to_datetime in the first place ...), "This is the end of the course. I'll forget it soon! It's dangerous! ", So I'd like to summarize it instead of a memorandum.

datetime module

Module for manipulating dates and times ・ Aware class: Data with information on time adjustment such as daylight saving time and time zone ・ Naive class: No data


Basic
datetime.datetime (datetime object) </ b> ・ ・ ・ Year / month / day / hour / minute / second (one object that has information on date object and time object)

from datetime import datetime
today = datetime(2020, 6, 1, 15, 30, 00)
print(today)

Output: 2020-06-01 15:30:00 

datetime can also be used when you want to convert the value of a POSIX timestamp (datetime.fromtimestamp).

datetime.date (date object) </ b> ・ ・ ・ Date object

from datetime import date
today = date(2020, 6, 1)
print(today)

Output: 2020-06-01

The methods that can be used are covered with datetime (reverse? What can be used with date objects can also be used with datetime ??). Fromtimestamp is also the same, and .isoformat.

datetime.timedelta (timedelta object) </ b> ・ ・ ・ Elapsed time, time difference, etc. (Duration)

from datetime import date, timedelta
today = date(2020, 6, 1)
print(today + timedelta(days=7))

Output: 2020-06-08

The arguments of timedelta () are days, hours, and so on. Works with negative numbers.

datetime.astimezone (astimezone object) </ b> ・ ・ ・ Apply the timezone specified in the argument.

from datetime import datetime, timezone
today = datetime(2020, 6, 1, 15, 30, 00)
today = today.astimezone(timezone.utc)
print(today)

Output: 2020-06-01 06:30:00+00:00

dateutil.tz module

dateutil is a package that extends datetime. This time, only the tz module, which is strong in timezone implementation learned by DataCamp.

tz.gettz () </ b> ・ ・ ・ Create an instance of the time zone in the argument.

from dateutil import tz
est = tz.gettz('America/New_York')
print(est)

Output: tzfile('/usr/share/zoneinfo/America/New_York')

If you use it alone, it will not come out well, but since the time zone of each region is pulled by the continent and city name, you can use it by putting it in the datetime object tzinfo.

tz.datetime_ambiguous () </ b>… An object that determines if there is an ambiguous time. "Ambiguous time" is the time that is mapped to multiple UTCs. There was a very easy-to-understand example, so please read this blog .

from dateutil import tz
from datetime import datetime

est = tz.gettz('America/New_York')
first_1am = datetime(2019, 11, 2, 1, 0, 0, tzinfo=est)
tz.datetime_ambiguous(first_1am)

Output: True //Should be. I got an Attribute Error in Pythonista3.

tz.enfold () </ b> ・ ・ ・ An object to distinguish the ambiguous time mentioned above. If you enter the daylight saving time as an argument, it will be returned in the time after the end of daylight saving time. I couldn't understand it even when I looked at the Document, so I guessed it from what I learned in DataCamp.

When working with date types in Pandas

I knew only pd.to_datetime, but there were many others.

pandas.DataFrame.tz_localize () </ b> ・ ・ ・ Localize naive DataFrame or Series to the specified time zone.

pandas.DataFrame.tz_convert () </ b> ・ ・ ・ Converts aware DataFrame or Series to the specified time zone.

At the end

I didn't implement much around dates, so I knew a lot for the first time. I also learned for the first time that tz database . I thought it was complicated and deep. I can't say anything because I don't know about other languages, but conversely, Python has various modules, and dates are easy to touch. I thought.

If the description is incorrect, it would be helpful if you could point it out (* ^^ *)

Reference document

・ Https://dateutil.readthedocs.io/en/stable/tz.html ・ Https://docs.python.org/3/library/datetime.html ・ Https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.tz_localize.html ・ Https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.tz_convert.html ・

Recommended Posts