On Tue, 2024-10-08 at 09:34 +0100, Kevin Sheppard via NumPy-Discussion
wrote:
> Can anyone shed some light on the expected behavior of code using
> array(..., copy=True) with pandas objects? We ran into this in
> statsmodels
> and I think there are probably plenty of places where we explicitly
> call
> array(..., copy=True) and think we should have a totally independent
> copy
> of the data. One workaround is to use
> np.require(...,requirements="O") but
> it would help to understand the expected behavior.


This is a bug in pandas, which added but ignored `copy=` in their
`__array__` methods:
    https://github.com/pandas-dev/pandas/issues/57739

It probably makes sense bumping there.  NumPy could assume it isn't
correctly implemented, but that would be weird also...
(Objects that did nothing would get correct behavior but with a
DeprecationWarning.)


- Sebastian


> 
> Here is a simple example:
> 
> import numpy as np
> import pandas as pd
> 
> weeks = 2
> now = pd.to_datetime('2024-01-01')
> testdata = pd.DataFrame(columns=['dates', 'values'])
> rg = np.random.default_rng(0)
> testdata['dates'] = pd.date_range(start=now, periods=weeks * 7,
> freq='D')
> testdata['values']=rg.integers(0, 100, size=(weeks * 7))
> 
> values = testdata['values']
> print("*"*10, " Before ", "*"*10)
> print(values.head())
> arr = np.array(values, copy=True)
> arr.sort()
> print("*"*10, " After ", "*"*10)
> print(values.head())
> print("*"*10, " Flags ", "*"*10)
> print(arr.flags)
> 
> This produces
> 
> **********  Before  **********
> 0    85
> 1    63
> 2    51
> 3    26
> 4    30
> Name: values, dtype: int64
> **********  After  **********
> 0     1
> 1     4
> 2     7
> 3    17
> 4    26
> Name: values, dtype: int64
> **********  Flags  **********
>   C_CONTIGUOUS : True
>   F_CONTIGUOUS : True
>   OWNDATA : False
>   WRITEABLE : True
>   ALIGNED : True
>   WRITEBACKIFCOPY : False
> 
> Thanks,
> Kevin
> _______________________________________________
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: sebast...@sipsolutions.net


_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to