

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract only the month and day from a datetime object in Python?
To extract only the month and day from a datetime object in Python, we can use the DateFormatter() class.
steps
Set the figure size and adjust the padding between and around the subplots.
Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.
Create a figure and a set of subplots.
Plot the dataframe using plot() method.
Set the axis formatter, extract month and day.
To display the figure, use show() method.
Example
import numpy as np import pandas as pd from matplotlib import pyplot as plt, dates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) fig, ax = plt.subplots() ax.plot(df.time, df.speed) ax.xaxis.set_major_formatter(dates.DateFormatter('M:%m\nD:%d')) plt.show()
Output
- Related Questions & Answers
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Extract day, hour, minute, etc. from a datetime column in PostgreSQL?
- MongoDB query to search for Month and Day only?
- How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?
- How to get only Date portion from DateTime object in C#?
- How to get a Date from year, month and day in Java?
- Compare only day and month with date field in MySQL?
- How to display first day and last day of the month from date records in MySQL?
- MongoDB query to search date records using only Month and Day
- How can we extract the Year and Month from a date in MySQL?
- MongoDB query to convert the field value and create datetime day of month during projection?
- How to get last day of a month in Python?
- MySQL query to extract only the day instead of entire date
- Python Pandas - Create a PeriodIndex and get the day of the month
- How to convert year, month, and day of the month into a complete date in R?
Advertisements