gerrymanoim / exchange_calendars

Calendars for various securities exchanges.
Apache License 2.0
396 stars 129 forks source link

ValueError: assignment destination is read-only in exchange_calendars/exchange_calendar.py", line 2907 when running with Pandas COW #379

Closed LaurensBosscher closed 1 week ago

LaurensBosscher commented 3 months ago

Pandas 3 will enable COW by default: https://pandas.pydata.org/docs/user_guide/copy_on_write.html.

In preparation of this (and to benefit from the performance/memory improvements) users can enable COW in any pandas >= 1.5 with the following settings:

import pandas as pd

pd.options.mode.copy_on_write = True

When doing so and calling xcals.get_calendar the following error is raised:

File "/code/libraries/Portfolio/portfolio_library/portfolio_constructor/portfolio_timeseries.py", line 361, in construct_portfolio_timeseries
cboe_futures_exchange_calendar = xcals.get_calendar("XCBF")
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/code/projects/investment_decision_support_system/.venv/lib/python3.11/site-packages/exchange_calendars/calendar_utils.py", line 294, in get_calendar
return cached if cached is not None else self._fabricate(name, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/code/projects/investment_decision_support_system/.venv/lib/python3.11/site-packages/exchange_calendars/calendar_utils.py", line 187, in _fabricate
calendar = factory(**kwargs)
^^^^^^^^^^^^^^^^^
File "/code/projects/investment_decision_support_system/.venv/lib/python3.11/site-packages/exchange_calendars/exchange_calendar.py", line 362, in __init__
_overwrite_special_dates(_all_days, self._closes, _special_closes)
File "/code/projects/investment_decision_support_system/.venv/lib/python3.11/site-packages/exchange_calendars/exchange_calendar.py", line 2907, in _overwrite_special_dates
standard_times.values[indexer] = special_times.values
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
ValueError: assignment destination is read-only

I checked the code and it does mention that it's a dirty hack: https://github.com/gerrymanoim/exchange_calendars/blob/7651212f915b8de87811c2d7ed1566944930475d/exchange_calendars/exchange_calendar.py#L2903C1-L2907C58

    # NOTE: This is a slightly dirty hack.  We're in-place overwriting the
    # internal data of an Index, which is conceptually immutable.  Since we're
    # maintaining sorting, this should be ok, but this is a good place to
    # sanity check if things start going haywire with calendar computations.
    standard_times.values[indexer] = special_times.values

I figured it would be worth opening an issue for this since it would be nice to fix this before pandas 3 is released so that users won't run into any issues.

maread99 commented 3 months ago

@LaurensBosscher, thank you for this heads up!!