Write an array to an NPY file, including a header. If the array is neither C-contiguous nor Fortran-contiguous AND the file_like object is not a real file object, this function will have to copy data in memory. Parameters ---------- fp : file_like object An ope
(fp, array, version=None, allow_pickle=True, pickle_kwargs=None)
| 664 | return d['shape'], d['fortran_order'], dtype |
| 665 | |
| 666 | def write_array(fp, array, version=None, allow_pickle=True, pickle_kwargs=None): |
| 667 | """ |
| 668 | Write an array to an NPY file, including a header. |
| 669 | |
| 670 | If the array is neither C-contiguous nor Fortran-contiguous AND the |
| 671 | file_like object is not a real file object, this function will have to |
| 672 | copy data in memory. |
| 673 | |
| 674 | Parameters |
| 675 | ---------- |
| 676 | fp : file_like object |
| 677 | An open, writable file object, or similar object with a |
| 678 | ``.write()`` method. |
| 679 | array : ndarray |
| 680 | The array to write to disk. |
| 681 | version : (int, int) or None, optional |
| 682 | The version number of the format. None means use the oldest |
| 683 | supported version that is able to store the data. Default: None |
| 684 | allow_pickle : bool, optional |
| 685 | Whether to allow writing pickled data. Default: True |
| 686 | pickle_kwargs : dict, optional |
| 687 | Additional keyword arguments to pass to pickle.dump, excluding |
| 688 | 'protocol'. These are only useful when pickling objects in object |
| 689 | arrays on Python 3 to Python 2 compatible format. |
| 690 | |
| 691 | Raises |
| 692 | ------ |
| 693 | ValueError |
| 694 | If the array cannot be persisted. This includes the case of |
| 695 | allow_pickle=False and array being an object array. |
| 696 | Various other errors |
| 697 | If the array contains Python objects as part of its dtype, the |
| 698 | process of pickling them may raise various errors if the objects |
| 699 | are not picklable. |
| 700 | |
| 701 | """ |
| 702 | _check_version(version) |
| 703 | _write_array_header(fp, header_data_from_array_1_0(array), version) |
| 704 | |
| 705 | if array.itemsize == 0: |
| 706 | buffersize = 0 |
| 707 | else: |
| 708 | # Set buffer size to 16 MiB to hide the Python loop overhead. |
| 709 | buffersize = max(16 * 1024 ** 2 // array.itemsize, 1) |
| 710 | |
| 711 | if array.dtype.hasobject: |
| 712 | # We contain Python objects so we cannot write out the data |
| 713 | # directly. Instead, we will pickle it out |
| 714 | if not allow_pickle: |
| 715 | raise ValueError("Object arrays cannot be saved when " |
| 716 | "allow_pickle=False") |
| 717 | if pickle_kwargs is None: |
| 718 | pickle_kwargs = {} |
| 719 | pickle.dump(array, fp, protocol=3, **pickle_kwargs) |
| 720 | elif array.flags.f_contiguous and not array.flags.c_contiguous: |
| 721 | if isfileobj(fp): |
| 722 | array.T.tofile(fp) |
| 723 | else: |
nothing calls this directly
no test coverage detected