Finding next month: Excel, Python

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
901
Hello the following returns "Feb 25"

today = datetime.datetime.today().date()
string_date = today.strftime("%b %y")

I now want to adapt so that it finds the next month from today, "Mar 25".

Any ideas, thanks.
 
So this is what I have for the current month - February 2025 - which works as a graph.

I have highlighted in bold the key bits.

Essentially string_date = Feb 25 in this example as its finding the current month and year from today's date.
I need to be able to find the next month, i.e. Mar 25.
long_string_date is just the title of the graph, e.g. February 2025.

I Wonder if I can just adapt this code?



import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker
from dateutil.relativedelta import relativedelta
import calendar

today = datetime.datetime.today().date()
string_date = today.strftime("%b %y")
long_string_date = today.strftime("%B %Y")


df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[(df["Date Captured"].dt.date == today) & (df["Mon Year"] == string_date)]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()

fig, ax = plt.subplots()
p1 = ax.bar(grouped["Check-In"], grouped["Current price"], color='g')
ax.bar_label(p1, label_type="edge")
ax.bar_label(p1)

# Formatting the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.xlabel("Check-In Date")
plt.ylabel("Price (£)")
plt.title(long_string_date)
plt.xticks(rotation=90) # Rotate labels
plt.ylim(50, 200)
plt.show()
 
Upvote 0
Solution:

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker
from dateutil.relativedelta import relativedelta
import calendar
import openpyxl
from openpyxl import Workbook

now = datetime.datetime.today().date()
lastmonth_date = now + timedelta(days=now.day)
lastmonth = lastmonth_date.strftime("%b %y")


today = datetime.datetime.today().date()
long_string_date = lastmonth_date.strftime("%B %Y")

df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[(df["Date Captured"].dt.date == today) & (df["Mon Year"] == lastmonth)]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()

fig, ax = plt.subplots()
p1 = ax.bar(grouped["Check-In"], grouped["Current price"], color='g')
ax.bar_label(p1, label_type="edge")
ax.bar_label(p1)

# Formatting the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.xlabel("Check-In Date")
plt.ylabel("Price (£)")
plt.title(long_string_date)
plt.xticks(rotation=90) # Rotate labels
plt.ylim(50, 200)
plt.show()
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top