Repository navigation
Description
Proposed new feature or change:
Recently I have suggested some new dtypes and during that discussion, we have also discussed the .npy file format, see #25374. As requested, I hereby open a new issue.
New datatypes for efficient storage (#25374) like ragged arrays might not be in sight anytime soon in Numpy, but what caught the least controversy was that .npy so far has been appendable (unless when using object dtype), my pull request for a minor change in the .npy header got accepted (#20321) and I have created a library to do the actual appending (npy-append-array). However, with how the upcoming stringdtype is currently specified (compare NEP 55), if there is going to be a new .npy format version 4 with data on the side (compare parameter sidecar_size), .npy files will not be appendable anymore (at least if stringdtype is used). Serialization for stringdtype has not been implemented or specified in-detail yet. A new Numpy file format version will be a breaking change and Numpy version 2.0 is on the horizon. Given these circumstances, I suggest a new option for .npy files: zip. According to the file format list on my Ubuntu 22.04, 56 popular file formats are based on zip:
cat /usr/share/mime/subclasses | grep "application/zip" | wc -l
This not only includes office file formats, but e.g. even Windows executables .exe files are .zip underneath - and they use it (amongst others) to distinguish between program code and strings. So plain zip may be considered to be super widespread among file formats and it is natively supported by Python.
How to do Arrays with zip? The simpliest way is to define a file hierarchy:
index.npy
strings.npy (optional)
So basically, what np.load would need to check is if the .npy file is a zip, then try to load index.npy within that file and further resources (like strings.npy) if required accordingly.
Depending on whether or not stringdtype can have its UTF8 string data in a regular .npy array (e.g. dtype char), the introduction of a new .npy file format version would even not be necessary. Theoretically, even users with old Numpy versions could just load the index.npy and strings.npy individually and be fine (although this may not matter). So the Numpy files within the zip file would still be of the current format (e.g. 1.0 or 3.0).
How does this make stringdtype files appendable? Analogous to zip, one could handle directories. So if a user tries to load a directory with np.load, it could look for an index.npy and load it instead. Now, both index.npy and strings.npy can be appended individually.
Therefore, the definition of what .npy files are would change:
- They can either be of the Numpy core file format as specified in NEP 1
- Or be zip, then they contain some
index.npyof the Numpy core file format - Or they can be a directory, then again the
index.npyof the Numpy core format would be used
In the future, those .npy zip files may contain more data. What comes to my mind (besides ragged arrays that already caused controversy) could maybe a common file format with Pandas to store dataframes? (I can already see the bullets flying towards me for that suggestion 😄) Currently, as far as I know, Pandas main file format is HDF, which is rather complex and causes a lot of issues. Especially with Numpy supporting stringdtype, some interesting options may open up here. But no matter what future use zip for Numpy might or might not have, it is a very simple and standard thing to do and it provides a lot of flexibility.
And finally, as a cherry on top so to say, zip can be (optionally) compressed. In many cases, compression can reduce the size of array data significantly, not only if the array is sparse. Users could profit a lot from that.
Any thoughts about that?
Merry Christmas everybody, Michael
Activity
rkern commentedon Dec 28, 2023
Some miscellaneous pronouncements to help shape the scope of the discussion:
Introducing a new format version is not a compatibility break, and has no relation to the numpy 2.0 release. We can introduce a new format version bump at any time; we have done so before. 2.0 will not be held up by this discussion.
Radical changes to the format are less likely to be a format version bump than a separate format entirely. But still would not constitute a compatibility break.
NPY appendability is a nice-to-have feature, not a must-have feature. If there are compelling reasons to use a way of serializing
stringdtypearrays that is not appendable, we'll do that.NPY support for
stringdtypeis also nice-to-have, not must-have (IMO; other core devs might have other opinions). If serialization via external libraries likepyarroware easier and better than trying to shoehorn it into the assumptions of the NPY format, I'm personally fine with that. But IMO, any NPY support is nicer-to-have than appendability.mmap-ability is a nice-to-have feature, but way down on the list. Because of the nature of variable-length strings, mmapability would solely be for lazy readonly use cases and not for writing, so half of the use cases for mmapability are already gone.
Sharing a serialization format with Pandas is an explicit non-goal. FWIW, they have considered and rejected NPY files inside of ZIP files already for good reasons. They are moving towards Arrow, which is a great fit for them, and if anything, I would drift NPY towards Arrow.
Okay, with that throat-clearing, I am generally negative on this idea. It's going to significant lengths to add a nice-to-have feature for a nicer-to-have-but-still-not-must-have
stringdtypesupport in NPY. In the scenario that you are proposing, you essentially have a 2-step process. You can't append to files inside of ZIPs (efficiently), so you have to start out with this second new format, the NPY-directory. Then when you are done appending, then you can ZIP it up into one file that you can then transfer around as an atomic unit. But if you are doing a 2-step procedure anyways, then a non-appendable NPY format forstringdtypewould work just fine. You could just as well write out individual chunks each as separate files into a directory during the appending process, and then convert things back into one non-appendable NPY with a second pass.If efficient appendability matters to you, I would strongly suggest that you consider Arrow-inside-NPY to be a strong contender. With the Arrow format, you can just keep appending chunks within the same file. I think it's a strong contender for other reasons, and the appendability is a nice-to-have. I do think that
stringdtypewill need to consider how to efficiently read from and write to Arrow streams in any case because that will be a huge use-case for interoperability. As far as I can tell, the Arrow scheme for storing strings is roughly along the same lines as the NEP-55-proposed option of an offset buffer and a sidecar buffer, but Arrow does it in chunks, and so is appendable/streamable. We don't need apyarrowdependency to do this, probably just a little bit of C-side support withstringdtypeto efficiently read and write these buffers as chunks. We'd want as much C-side support for the NEP-55-proposed option, too. We can take care of the rest of the ArrowRecordBatchformat gunk inside of the NPY code, no problem.ngoldbaum commentedon Dec 28, 2023
For what it's worth, I would rather avoid updating the NPY file format and defining new semantics for data storage to get stringdtype out the door.
The current situation, where stringdtype supports pickle storage but not other kinds of storage, is the same as the status quo for object arrays.
Given your prompting and Robert's message above, my current plan is to revise the NEP to leave out the section on updating the NPY format and ship stringdtype only supporting pickle serialization, with an eye towards resolving this by, for example, adding a way to expose the data via the arrow C interface or the dlpack interface.