FiscalYear
FiscalYear is a small, lightweight Python module (current version 0.4.0) that provides utilities for managing the fiscal calendar. It extends Python's built-in `datetime` and `calendar` modules, enabling users to query the fiscal year, quarter, month, and day for any given date or datetime object. The project sees releases periodically, including bug fixes, new features, and occasional breaking changes to improve functionality and maintainability.
Common errors
-
TypeError: FiscalDate() takes 3 positional arguments but 4 were given
cause Attempting to pass a `datetime.datetime` or `datetime.date` object directly to `FiscalDate()` instead of its constituent year, month, and day components.fixPass the year, month, and day as separate integer arguments: `FiscalDate(my_datetime_obj.year, my_datetime_obj.month, my_datetime_obj.day)`. -
AttributeError: module 'fiscalyear' has no attribute 'quarter'
cause Using the deprecated `quarter` attribute or method, which was removed in version 0.4.0.fixReplace `object.quarter` with `object.fiscal_quarter`. For previous/next, use `prev_fiscal_quarter` and `next_fiscal_quarter`. -
The fiscalyear.setup_fiscal_calendar(start_month=X) does not correctly apply the start month, resulting in incorrect fiscal year/quarter calculations.
cause While a specific bug related to `fiscal_month` and `START_DAY` was fixed in v0.3.2, user reports indicate ongoing issues or confusion with `setup_fiscal_calendar`'s application, potentially due to incorrect usage or interaction with other settings.fixEnsure `setup_fiscal_calendar` is called early in your application's lifecycle, preferably before any `FiscalYear` or `FiscalDate` objects are instantiated. Double-check the `start_year`, `start_month`, and `start_day` parameters. For temporary changes, use the `fiscal_calendar` context manager.
Warnings
- breaking The methods `quarter`, `prev_quarter`, and `next_quarter` were deprecated in v0.3.0 and completely removed in v0.4.0. Using them will result in an `AttributeError`.
- breaking Python 2 support was dropped in `fiscalyear` v0.4.0. The library now requires Python 3.6 or newer.
- breaking As of v0.4.0, many classes and functions that previously accepted 'int-like strings' for date components (e.g., '10' for October) now strictly require `int` types. Providing strings will lead to `TypeError` or `ValueError`.
- gotcha Direct modification of `fiscalyear` global variables (e.g., `fiscalyear.START_MONTH = 7`) is discouraged and may lead to issues or be removed in future releases. It can also cause unexpected behavior in multi-threaded environments or when working with multiple fiscal calendars.
Install
-
pip install fiscalyear -
conda install -c conda-forge fiscalyear
Imports
- FiscalYear
from fiscalyear import FiscalYear
- FiscalQuarter
from fiscalyear import FiscalQuarter
- FiscalMonth
from fiscalyear import FiscalMonth
- FiscalDay
from fiscalyear import FiscalDay
- FiscalDate
from fiscalyear import FiscalDate
- setup_fiscal_calendar
from fiscalyear import setup_fiscal_calendar
- fiscal_calendar
from fiscalyear import fiscal_calendar
Quickstart
import datetime
from fiscalyear import FiscalYear, FiscalDate, setup_fiscal_calendar, fiscal_calendar
# Set fiscal year to start in July globally
setup_fiscal_calendar(start_month=7)
# Get current fiscal year information
current_fy = FiscalYear.current()
print(f"Current Fiscal Year: {current_fy.year}")
print(f"FY Start: {current_fy.start.date()}, FY End: {current_fy.end.date()}")
# Get fiscal info for a specific date
some_date = datetime.date(2024, 11, 15)
f_date = FiscalDate(some_date.year, some_date.month, some_date.day)
print(f"\nDate: {f_date}")
print(f"Fiscal Year: {f_date.fiscal_year}")
print(f"Fiscal Quarter: {f_date.fiscal_quarter}")
print(f"Fiscal Month: {f_date.fiscal_month}")
# Temporarily change fiscal calendar using context manager
with fiscal_calendar(start_month=4):
temp_f_date = FiscalDate(2024, 11, 15)
print(f"\nInside context manager (April start) for {temp_f_date}:")
print(f"Temporary Fiscal Year: {temp_f_date.fiscal_year}")
print(f"Temporary Fiscal Quarter: {temp_f_date.fiscal_quarter}")
# Outside context manager, global setting (July start) is restored
print(f"\nOutside context manager: Fiscal Year for {f_date}: {f_date.fiscal_year}")