Skip to content

Commit b6ac250

Browse files
committed
#27: supports pandas copy_on_write to prepare for pandas 3.0
1 parent f526556 commit b6ac250

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export STOCK_PANDAS_BUILDING = 1
66
export STOCK_PANDAS_UPLOADING = 1
77

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

1111
lint:
1212
ruff check $(files)

stock_pandas/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
cumulators
2121
)
2222

23-
__version__ = '1.2.7'
23+
__version__ = '1.3.0'

stock_pandas/dataframe.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from typing import (
24
Any,
35
Callable,
@@ -8,6 +10,7 @@
810
Optional
911
)
1012

13+
import pandas as pd
1114
from pandas import (
1215
Series
1316
)
@@ -33,6 +36,12 @@
3336
MetaDataFrame
3437
)
3538

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

3746
class StockDataFrame(MetaDataFrame):
3847
"""The wrapper class for `pandas.DataFrame`
@@ -326,23 +335,10 @@ def _get_or_calc_series(
326335
period
327336
)
328337

329-
self._set_new_item(name, array)
338+
self.loc[:, name] = array
330339

331340
return name, array
332341

333-
def _set_new_item(
334-
self,
335-
name: str,
336-
value: ndarray
337-
) -> None:
338-
"""Set a new column and avoid SettingWithCopyWarning by using
339-
pandas internal APIs
340-
341-
see: https://github.com/pandas-dev/pandas/blob/v1.1.0/pandas/core/frame.py#L3114
342-
"""
343-
344-
self._set_item(name, value)
345-
346342
def _fulfill_series(self, column_name: str) -> ndarray:
347343
column_info = self._stock_columns_info_map.get(column_name)
348344
size = len(self)
@@ -369,9 +365,15 @@ def _fulfill_series(self, column_name: str) -> ndarray:
369365
if neg_delta == calc_delta:
370366
array = partial
371367
else:
368+
# #27
369+
# With `pd.options.mode.copy_on_write = True`,
370+
# Series.to_numpy() will returns the
371+
# read-only underlying numpy array of the Series
372+
# so we need to copy it before modifying
373+
array = array.copy()
372374
array[fulfill_slice] = partial[fulfill_slice]
373375

374-
self._set_new_item(column_name, array)
376+
self.loc[:, column_name] = array
375377

376378
column_info.size = size
377379

0 commit comments

Comments
 (0)