Skip to content

BUG: Pickling/unpickling datetime64 adds dtype metadata #29344

@sambateman

Description

@sambateman

Describe the issue:

I have an array with dtype datetime64. The dtype.metadata is None. After serializing the array with pickle dump/load, the dtype.metadata is some object "mappingproxy({})".

This is a problem because trying to np.save the array with dtype.metadata produces a UserWarning.

dtype.metadata appears to be some kind of undocumented feature(???) I've never heard of before now.

Reproduce the code example:

import numpy as np
import pickle
import io

a = np.asarray([np.datetime64('2000-01-01')])
print(f'{a = }')
print(f'{a.dtype = }')
print(f'{a.dtype.metadata = }')

b = pickle.loads(pickle.dumps(a))
print(f'{b = }')
print(f'{b.dtype = }')
print(f'{b.dtype.metadata = }')

print('Trying to save a')
f = io.BytesIO()
np.save(f, a)

print('Trying to save b')
f = io.BytesIO()
np.save(f, b)

Error message:

a = array(['2000-01-01'], dtype='datetime64[D]')
a.dtype = dtype('<M8[D]')
a.dtype.metadata = None
b = array(['2000-01-01'], dtype='datetime64[D]')
b.dtype = dtype('<M8[D]')
b.dtype.metadata = mappingproxy({})
Trying to save a
Trying to save b
<...>\site-packages\numpy\lib\_format_impl.py:393: UserWarning: metadata on a dtype is not saved to an npy/npz. Use another format (such as pickle) to store it.
d['descr'] = dtype_to_descr(array.dtype)

Python and NumPy Versions:

Numpy version
2.3.1

Python version
3.13.5 | packaged by conda-forge | (main, Jun 16 2025, 08:20:19)

Runtime Environment:

No response

Context for the issue:

I'm trying to do some multiprocessing with numpy arrays. The multiprocessing.Pool.map() uses pickle for serialization, which led to this issue.

It's pretty surprising that a visible UserWarning is produced because of some internal magic undocumented metadata silliness.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy