Skip to content

BUG: np.asarray return a copy with shared memory #24478

@Itayazolay

Description

@Itayazolay

Describe the issue:

According to np.asarray(a) docs, it may return the original array or a copy.
In the case below, while np.asarray(a, dtype='object') is not a, - np.shares_memory(np.asarray(a, dtype='object'), a) given that a is copied.

Reproduce the code example:

import numpy as np
import pickle
t = np.array([None, None, 123], dtype='object')
tp = pickle.loads(pickle.dumps(t))
g = np.asarray(tp, dtype='object')

assert g is not tp
assert np.shares_memory(g, tp)  # Unexpected
g[0] = '1'
assert tp[0] == '1'  # Unexpected


# The issue seems to be originated here
assert not np.dtype('O') is pickle.loads(pickle.dumps(np.dtype('O'))) 
assert np.dtype('O') == pickle.loads(pickle.dumps(np.dtype('O')))

Error message:

No response

Runtime information:

1.25.2
3.10.11 | packaged by conda-forge | (main, May 10 2023, 18:58:44) [GCC 11.3.0]
[{'numpy_version': '1.25.2',
'python': '3.10.11 | packaged by conda-forge | (main, May 10 2023, 18:58:44) '
'[GCC 11.3.0]',
'uname': uname_result(system='Linux', node='itay-jether', release='6.2.0-26-generic', version='#26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL'],
'not_found': ['AVX512_KNL', 'AVX512_KNM']}},
{'architecture': 'SkylakeX',
'filepath': '/home/itay/miniforge3/envs/jpy310/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-5007b62f.3.23.dev.so',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'},
{'architecture': 'SkylakeX',
'filepath': '/home/itay/miniforge3/envs/jpy310/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.18'},
{'architecture': 'SkylakeX',
'filepath': '/home/itay/miniforge3/envs/jpy310/lib/python3.10/site-packages/cvxopt.libs/libopenblasp-r0-5c2b7639.3.23.so',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23'},
{'architecture': 'Prescott',
'filepath': '/home/itay/miniforge3/envs/jpy310/lib/python3.10/site-packages/scs.libs/libopenblas-r0-f650aae0.3.3.so',
'internal_api': 'openblas',
'num_threads': 1,
'prefix': 'libopenblas',
'threading_layer': 'disabled',
'user_api': 'blas',
'version': None},
{'filepath': '/home/itay/miniforge3/envs/jpy310/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0',
'internal_api': 'openmp',
'num_threads': 8,
'prefix': 'libgomp',
'user_api': 'openmp',
'version': None}]

Context for the issue:

I'm using pandas and have pickled dataframes, when using df.astype(str) the dataframe is changed in-place
see issue in pandas

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions