Change language

Python | Pandas Series.dt.strftime

|

Series.dt can be used to access series values ​​as datetimelike and return multiple properties. Series.dt.strftime() Pandas Series.dt.strftime() is used to convert to index using of the specified date_format. The function returns the index of the formatted strings specified by the date_format parameter, which supports the same string format as the python standard library.

Syntax: Series.dt.strftime (* args , ** kwargs)

Parameter:
date_format: Date format string (eg “% Y-% m-% d”)

Returns: Index of formatted strings

Example # 1: Use Series.dt. strftime () to convert dates in this series object to the specified date format.

# import pandas as pd

import pandas as pd

  
# Create series

sr = pd.Series ([ ’ 2012-12-31 08:45’ , ’2019-1-1 12:30’ , ’ 2008-02-2 10:30’ ,

’2010-1- 1 09: 25’ , ’2019-12-31 00: 00’ ])

  
# Create index

idx = [ ’Day 1’ , ’ Day 2’ , ’Day 3’ , ’ Day 4’ , ’Day 5’ ]

 < / code> 
# set index

sr.index = idx

  
# Convert base data to date and time

sr = pd.to_datetime (sr)

 
# Print episode

print (sr)

Output:

We will now use Series.dt.strftime () to convert the dates in the specified series object to the specified format.

# convert to the specified date format

result = sr.dt.strftime ( ’% B% d,% Y,% r’ )

 
# print the result

print (result)

Output:

As we can see in the output, Series.dt.strftime () successfully converted the dates in this series object to the specified format.

Example # 2: Use Series.dt.strftime () to convert the dates in the specified series object to the specified date format.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series (pd.date_range ( ’2012- 12-31 09:45’ , periods = 5 , freq = ’M’ ,

tz = ’Asia / Calcutta’ ))

  
# Create index

idx = [ ’ Day 1’ , ’Day 2’ , ’Day 3’ , ’ Day 4’ , ’Day 5’ ]

 
# set index

sr.index = idx

 
# Print series

print (sr)

Output:

Now we will use Series.dt.strftime () to convert dates in the specified series object to the specified format.

 

# convert to the specified date format

result = sr.dt.strftime ( ’% d% m% Y,% r’ )

 
# print result

print (result)

Output:

As we can see in the output, Series.dt.strftime () successfully converted the dates in this series object to the specified format.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method