Python API

ndcsv.write_csv(array: DataArray | Series | DataFrame, path_or_buf: str | IO) None
ndcsv.write_csv(array: DataArray | Series | DataFrame, path_or_buf: Literal[None] = None) str

Write an n-dimensional array to an NDCSV file.

Any number of dimensions are supported. If the array has more than two dimensions, all dimensions beyond the first are automatically stacked together on the columns of the CSV file; if you want to stack dimensions on the rows you’ll need to manually invoke xarray.DataArray.stack() beforehand.

This function is conceptually similar to pandas.DataFrame.to_csv(), except that none of the many configuration settings is made available to the end user, in order to ensure consistency in the output file.

Parameters:
  • array

    One of:

  • path_or_buf

    One of:

    • .csv file path

    • .csv.gz / .csv.bz2 / .csv.xz file path (the compression algorithm is inferred automatically)

    • file-like object open for writing

    • None (the result is returned as a string)

ndcsv.read_csv(path_or_buf: str | TextIO, unstack: bool = True) DataArray

Parse an NDCSV file into a xarray.DataArray.

This function is conceptually similar to pandas.read_csv(), except that it only works for files that are strictly formatted according to File format specifications and, by design, does not offer any of the many config switches available in pandas.read_csv().

Parameters:
  • path_or_buf

    One of:

    • .csv file path

    • .csv.gz / .csv.bz2 / .csv.xz file path (the compression algorithm is inferred automatically)

    • file-like object open for reading. It must support rewinding through seek(0).

  • unstack (bool) –

    Set to True (the default) to automatically unstack any and all stacked dimensions in the output xarray, using first-seen order. Note that this differs from xarray.DataArray.unstack(), which may occasionally use alphabetical order instead. All indices must be unique for the unstack to succeed. Non-index coords can be duplicated.

    Set to False to return the stacked dimensions as they appear in the CSV file.

Returns:

xarray.DataArray