Pandas Timestamp Get Day


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.



Source link