Pandas provide us with the day attribute that allows extracting the day from a given timestamp object.

Usage

To use this attribute, you need to construct a Timestamp object. You can do this using the timestamp() function as shown:

1

2

3

4

# import pandas

import pandas as pd


ts = pd.Timestamp(‘2022-04-04’)

print(ts)

The above example creates a Pandas timestamp object from a datetime-time string. The resulting output is as shown:

You can explore the Pandas timestamp() function in the resource shown:

https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.html

Extract Day From Timestamp Object

To extract the day from the timestamp object shown above, we can run the code as shown:

1

print(f“date: {ts.day}”)

The above should return the day from the provided timestamp object.

Get Day of Week

You can also fetch the day name from a timestamp object using the day_name() function.

An example is as shown:

1

print(f“day: {ts.day_name()}”)

Closing

This was a short tutorial depicting how to extract the day and day name from a timestamp object.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/john-150×150.png629d79cd7aa51.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list