w3resource

Pandas: Business quarterly begin and end dates of a specified year

Pandas Time Series: Exercise-22 with Solution

Write a Pandas program to find the all the business quarterly begin and end dates of a specified year.

Sample Solution:

Python Code :

import pandas as pd
q_start_dates = pd.date_range('2020-01-01', '2020-12-31', freq='BQS-JUN')
q_end_dates = pd.date_range('2020-01-01', '2020-12-31', freq='BQ-JUN')
print("All the business quarterly begin dates of 2020:")
print(q_start_dates.values)
print("\nAll the business quarterly end dates of 2020:")
print(q_end_dates.values)

Sample Output:

All the business quarterly begin dates of 2020:
['2020-03-02T00:00:00.000000000' '2020-06-01T00:00:00.000000000'
 '2020-09-01T00:00:00.000000000' '2020-12-01T00:00:00.000000000']

All the business quarterly end dates of 2020:
['2020-03-31T00:00:00.000000000' '2020-06-30T00:00:00.000000000'
 '2020-09-30T00:00:00.000000000' '2020-12-31T00:00:00.000000000']

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to calculate all Thursdays between two given days.
Next: Write a Pandas program to generate sequences of fixed-frequency dates and time spans intervals.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}