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
datetimerefers 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.
datetimemodule in Python’sstandard librarycontains 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 similarto 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’
datetimedata in pandas, it’s
vectorization capabilities. Therefore, although youcanuse
generally better to use NumPy or pandas datetime objects when working in pandas, if possible.
datetime64and
NumPy’s datetime classesinclude, most notably, timedelta64
. Like
datetimeobjects,
datetime64objects contain dateand time information in a single data
timedeltaobjects,
structure; and, like timedelta64objects contain information pertaining
to spans of time.
Timestamp
Pandas’ datetime classesinclude Timedelta
, Period
, DateOffset
, and .
Because these classes are efficient and dynamic in their capabilities, you often don’t need to
datetimemodule when working with datetimedata 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
datetime64data using the
This series contains string data, but it can be converted to
pd.to_datetime()function:
my_series = pd.to_datetime
(
my_series
)
my_series
0
2023-01-20
1
2023-04-27
2
2023-06-15
dtype: datetime64[ns]
Refer to thepandas to_datetime() documentationformore information about this function.
Seriesobject contains datetime data, you canuse
When a dtto access various properties of
the data. For example:
print
(
my_series.[Link]
)
print
()
print
(
my_series.[Link]
)
print
()
print
(
my_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
datetimemodule from Python’s standard library as
Note that it’s not uncommon to import the
dt
dtis being used as an alias. The
. You may have encountered this yourself. In suchcase,
dtSeries accessor (as demonstrated in thelast example) is a different thing entirely.
pandas
Refer to thepandas dt accessor documentationformore 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.