Skip to content

Commit

Permalink
#27: supports pandas copy_on_write to prepare for pandas 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelzhang committed Oct 11, 2024
1 parent f526556 commit b6ac250
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export STOCK_PANDAS_BUILDING = 1
export STOCK_PANDAS_UPLOADING = 1

test:
pytest -s -v test/test_$(test_files).py --doctest-modules --cov stock_pandas --cov-config=.coveragerc --cov-report term-missing
STOCK_PANDAS_COW=1 pytest -s -v test/test_$(test_files).py --doctest-modules --cov stock_pandas --cov-config=.coveragerc --cov-report term-missing

lint:
ruff check $(files)
Expand Down
2 changes: 1 addition & 1 deletion stock_pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
cumulators
)

__version__ = '1.2.7'
__version__ = '1.3.0'
32 changes: 17 additions & 15 deletions stock_pandas/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from typing import (
Any,
Callable,
Expand All @@ -8,6 +10,7 @@
Optional
)

import pandas as pd
from pandas import (
Series
)
Expand All @@ -33,6 +36,12 @@
MetaDataFrame
)

# #27
if os.environ.get('STOCK_PANDAS_COW', '').lower() in ('1', 'on', 'true'):
# Enable pandas Copy-on-Write mode
# https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#copy-on-write-chained-assignment
pd.options.mode.copy_on_write = True


class StockDataFrame(MetaDataFrame):
"""The wrapper class for `pandas.DataFrame`
Expand Down Expand Up @@ -326,23 +335,10 @@ def _get_or_calc_series(
period
)

self._set_new_item(name, array)
self.loc[:, name] = array

return name, array

def _set_new_item(
self,
name: str,
value: ndarray
) -> None:
"""Set a new column and avoid SettingWithCopyWarning by using
pandas internal APIs
see: https://github.com/pandas-dev/pandas/blob/v1.1.0/pandas/core/frame.py#L3114
"""

self._set_item(name, value)

def _fulfill_series(self, column_name: str) -> ndarray:
column_info = self._stock_columns_info_map.get(column_name)
size = len(self)
Expand All @@ -369,9 +365,15 @@ def _fulfill_series(self, column_name: str) -> ndarray:
if neg_delta == calc_delta:
array = partial
else:
# #27
# With `pd.options.mode.copy_on_write = True`,
# Series.to_numpy() will returns the
# read-only underlying numpy array of the Series
# so we need to copy it before modifying
array = array.copy()
array[fulfill_slice] = partial[fulfill_slice]

self._set_new_item(column_name, array)
self.loc[:, column_name] = array

column_info.size = size

Expand Down

0 comments on commit b6ac250

Please sign in to comment.