0% found this document useful (0 votes)
13 views6 pages

Reference Guide - Datetime Manipulation

This document serves as a reference guide for manipulating datetime strings in Python, providing shorthand codes and examples for various datetime functions. It also discusses the use of datetime in NumPy and pandas, highlighting the efficiency of pandas' datetime classes. Key takeaways emphasize the importance of using reference guides for datetime manipulation throughout a data professional's career.

Uploaded by

artchromatica335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Reference Guide - Datetime Manipulation

This document serves as a reference guide for manipulating datetime strings in Python, providing shorthand codes and examples for various datetime functions. It also discusses the use of datetime in NumPy and pandas, highlighting the efficiency of pandas' datetime classes. Key takeaways emphasize the importance of using reference guides for datetime manipulation throughout a data professional's career.

Uploaded by

artchromatica335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Reference guide: Datetime manipulation‬

‭The following tables can serve as reference guides to remind you of the shorthand code for‬
‭manipulating datetime strings into individual objects.‬

‭Manipulating datetime strings in Python‬

‭Below, you will find a table with the datetime functions you can use to help you manipulate‬
‭datetime objects in different ways.‬

‭Code‬ ‭Format‬ ‭Example‬

‭%a‬ ‭Abbreviated workday‬ ‭Sun‬

‭%A‬ ‭Weekday‬ ‭Sunday‬

‭%b‬ ‭Abbreviated month‬ ‭Jan‬

‭%B‬ ‭Month name‬ ‭January‬

‭%c‬ ‭Date and time‬ ‭Sun Jan 1 [Link] 2021‬

‭%d‬ ‭Day (leading zeros)‬ ‭01 to 31‬

‭%H‬ ‭24 hours‬ ‭00 to 23‬

‭%I‬ ‭12 hours‬ ‭01 to 12‬

‭%j‬ ‭Day of year‬ ‭001 to 366‬

‭%m‬ ‭Month‬ ‭01 to 12‬

‭%M‬ ‭Minute‬ ‭00 to 59‬

‭%p‬ ‭AM or PM‬ ‭AM/PM‬

‭%S‬ ‭Seconds‬ ‭00 to 61‬

‭%U‬ ‭Week number (Sun)‬ ‭00 to 53‬

‭%W‬ ‭Week number (Mon)‬ ‭00 to 53‬

‭%w‬ ‭Weekday‬ ‭0 to 6‬

‭%x‬ ‭ ocale’s appropriate date‬


L ‭ 8/16/88 (None);‬
0
‭representation‬ ‭08/16/1988 (en_US);‬
‭16.08.1988 (de_DE)‬
‭%X‬ ‭ locale’s appropriate time‬ 2
A ‭ [Link] (en_US);‬
‭representation‬ ‭[Link] (de_DE)‬

‭%y‬ ‭Year without century‬ ‭00 to 99‬

‭%Y‬ ‭Year‬ ‭2022‬

‭%z‬ ‭Offset‬ ‭+0900‬

‭%Z‬ ‭Time zone‬ ‭EDT/JST/WET etc (GMT)‬


‭Datetime functions to remember‬
‭All of the following date string manipulations require the datetime package to be imported first.‬

‭Code‬ ‭Input Type‬ ‭Input Example‬ ‭Output‬ ‭Output‬


‭Type‬ ‭Example‬

‭[Link](“25/11/2022”,‬
d string‬
‭ “25/11/2022”‬
‭ ‭DateTime‬ ‭2022-11-25‬

“%d/%m/%Y”)‬
‭ ‭[Link]”‬

‭[Link](dt_object,‬
d DateTime‬
‭ ‭2022-11-25‬
“ ‭string‬ ‭“25/11/2022”‬
“%d/%m/%Y”)‬
‭ [Link]”‬

‭t_object =‬
d string‬
‭ “25/11/2022”‬
‭ ‭loat (UTC‬ ‭1617836400.0‬
f
[Link](“25/11/2022”,‬
‭ ‭timestamp‬
“%d/%m/%Y”)‬
‭ ‭in‬
[Link](dt_object)‬
‭ ‭seconds)‬

‭[Link](“25/11/2022”,‬
d string‬
‭ “25/11/2022”‬
‭ ‭string‬ ‭“2022-11-25”‬
“%d/%m/%Y”).strftime(“%Y-%m-%d”)‬

‭[Link](1617836400.‬ f
d ‭loat‬ 1617836400.0‬
‭ ‭DateTime‬ ‭[Link]‬
d
0)‬
‭ (UTC‬
‭ ‭time(2021, 4,‬
timestamp‬
‭ ‭7, 23, 0)‬
in‬

seconds)‬

‭[Link](1617836400.‬ f
d ‭loat‬ 1617836400.0‬
‭ ‭string‬ ‭'07/04/2021'‬

0).strftime(“%d/%m/%Y”)‬
‭ (UTC‬
‭ ‭”‬
timestamp‬

in‬

seconds)‬

‭rom pytz import timezone‬


f string‬
‭ ‭ewYork‬
N ‭DateTime‬ ‭okyo‬
T
ny_time =‬
‭ timezone‬
‭ ‭timezone‬
[Link](“25-11-2022‬
‭ “25-11-2022‬
‭ ‭2022, 11, 26,‬
[Link]-0700”, “%d-%m-%Y‬
‭ [Link]-0700‬
‭ ‭1, 34,‬
%H:%M:%S%z”)‬
‭ ”‬
‭ ‭JST+[Link]‬
Tokyo_time =‬
‭ ‭STD>‬
ny_time.astimezone(timezone(‘Asia/‬

Tokyo’))‬

‭[Link](“20:00”,‬
d string‬
‭ “20:00”‬
‭ ‭string‬ ‭“08:00 PM”‬
“%H:%M”).strftime(“%I:%M %p”)‬

‭[Link](“08:00 PM”,‬
d string‬
‭ “08:00 PM”‬
‭ ‭string‬ ‭“20:00”‬
“%I:%M %p”).strftime(“%H:%M”)‬

‭Datetime in NumPy and pandas‬
datetime‬‭refers to the specific‬
‭A preface regarding terminology in the following section:‬‭
‭module of that name in the Python standard library or to the specific class within that module.‬
‭Datetime (or uncapitalized, datetime) refers to any date/time-related object from any library or‬
‭language.‬

datetime‬‭module in Python’s‬‭standard library‬‭contains a number of‬


‭You’ve learned that the‬‭
date‬
‭classes used to work with time data, including‬‭ time‬
‭,‬‭ datetime‬
‭,‬‭ timedelta‬
‭,‬‭ ‭,‬
timezone‬
‭ tzinfo‬
‭, and‬‭ ‭. Remember, modules are similar‬‭to libraries, in that they are groups of‬
‭related classes and functions, but they are generally subcomponents of libraries. Classes are‬
‭data types that bundle data and functionality together.‬

‭NumPy and pandas have their own datetime classes that offer significant performance boosts‬
‭when working with large datasets. Pandas datetime classes, like the rest of the pandas library,‬
‭are built on NumPy. These classes have very similar (and in many cases identical) functionality‬
‭to Python’s native datetime classes, but they run more efficiently due to NumPy and pandas’‬
datetime‬‭data in pandas, it’s‬
‭vectorization capabilities. Therefore, although you‬‭can‬‭use‬‭
‭generally better to use NumPy or pandas datetime objects when working in pandas, if possible.‬

datetime64‬‭and‬‭
‭NumPy’s datetime classes‬‭include, most notably,‬‭ timedelta64‬
‭. Like‬
datetime‬‭objects,‬‭
‭ datetime64‬‭objects contain date‬‭and time information in a single data‬
timedelta‬‭objects,‬‭
‭structure; and, like‬‭ timedelta64‬‭objects contain information pertaining‬
‭to spans of time.‬

Timestamp‬
‭Pandas’ datetime classes‬‭include‬‭ Timedelta‬
‭,‬‭ Period‬
‭,‬‭ DateOffset‬
‭, and‬‭ ‭.‬
‭Because these classes are efficient and dynamic in their capabilities, you often don’t need to‬
datetime‬‭module when working with datetime‬‭data in pandas. Also, pandas will‬
‭import the‬‭
‭automatically recognize datetime-like data and convert it to the appropriate class when‬
‭possible. Here’s an example:‬

data =‬‭
‭ [‬'2023-01-20'‬
‭ ,‬‭
‭ '2023-04-27'‬
,‬‭
‭ '2023-06-15'‬
]‬

my_series = [Link]‬
‭ (‬
‭ data‬
‭ )‬

my_series‬

0
‭ 2023-01-20‬
1
‭ 2023-04-27‬
2
‭ 2023-06-15‬
dtype: object‬

datetime64‬‭data using the‬


‭This series contains string data, but it can be converted to‬‭
pd.to_datetime()‬‭function:‬

my_series = pd.to_datetime‬
‭ (‭
‭m‬y_series‬
)‬

my_series‬

0
‭ 2023-01-20‬
1
‭ 2023-04-27‬
2
‭ 2023-06-15‬
dtype: datetime64[ns]‬

‭Refer to the‬‭pandas to_datetime() documentation‬‭for‬‭more information about this function.‬


Series‬‭object contains datetime data, you can‬‭use‬‭
‭When a‬‭ dt‬‭to access various properties of‬
‭the data. For example:‬
print‬
‭ (‭
‭m‬y_series.[Link]‬
)‬

print‬
‭ ()‬

print‬
‭ (‭
‭m‬y_series.[Link]‬
)‬

print‬
‭ ()‬

print‬
‭ (‭
‭m‬y_series.[Link]‬
)‬

0
‭ 2023‬
1
‭ 2023‬
2
‭ 2023‬
dtype: int64‬

0
‭ 1‬
1
‭ 4‬
2
‭ 6‬
dtype: int64‬

0
‭ 20‬
1
‭ 27‬
2
‭ 15‬
dtype: int64‬

datetime‬‭module from Python’s standard library as‬
‭Note that it’s not uncommon to import the‬‭
dt‬
‭ dt‬‭is being used as an alias. The‬
‭. You may have encountered this yourself. In such‬‭case,‬‭
dt‬‭Series accessor (as demonstrated in the‬‭last example) is a different thing entirely.‬
‭pandas‬‭
‭Refer to the‬‭pandas dt accessor documentation‬‭for‬‭more information.‬

‭Key takeaways‬

‭Use reference guides like the tables above throughout your career to help remind you of the‬
‭different ways to manipulate datetime objects. Even experts in the field use reference guides,‬
‭rather than memorizing all this information. Getting familiar with guides like these will be‬
‭beneficial because you will be using them throughout your career as a data professional.‬

You might also like