metacsv.core package

Submodules

metacsv.core.containers module

class metacsv.core.containers.DataFrame(*args, **kwargs)[source]

Bases: metacsv.core.internals.Container, pandas.core.frame.DataFrame

metacsv.DataFrame, inherrited from pandas.DataFrame

Keyword Arguments:
 
  • attrs – dict-like Attributes of this container
  • coords – list or dict-like Coordinate dependencies
  • variables – dict-like Variable-specific attributes

*args, **kwargs are passed to pandas.DataFrame.__init__

copy()[source]
pandas_parent

alias of DataFrame

class metacsv.core.containers.Panel(*args, **kwargs)[source]

Bases: metacsv.core.internals.Container, pandas.core.panel.Panel

metacsv.Panel, inherrited from pandas.Panel

Keyword Arguments:
 
  • attrs – dict-like Attributes of this container
  • coords – list or dict-like Coordinate dependencies
  • variables – dict-like Variable-specific attributes

*args, **kwargs are passed to pandas.Panel.__init__

Note

metacsv.Panel is not fully implemented

copy()[source]
pandas_parent

alias of Panel

class metacsv.core.containers.Series(*args, **kwargs)[source]

Bases: metacsv.core.internals.Container, pandas.core.series.Series

metacsv.Series, inherrited from pandas.Series

Keyword Arguments:
 
  • attrs – dict-like Attributes of this container
  • coords – list or dict-like Coordinate dependencies
  • variables – dict-like Variable-specific attributes

*args, **kwargs are passed to pandas.Series.__init__

copy()[source]
pandas_parent

alias of Series

metacsv.core.exceptions module

exception metacsv.core.exceptions.GraphIsCyclicError[source]

Bases: exceptions.ValueError

metacsv.core.internals module

class metacsv.core.internals.Attributes(data=None, container=None)[source]

Bases: metacsv.core.internals._BaseProperty

property_type = u'Attributes'
class metacsv.core.internals.Container(coords=None, variables=None, attrs=None, *args, **kwargs)[source]

Bases: object

Base class for metacsv Container objects

Parameters:
  • coords (dict) – Container coordinates
  • variables (dict) – Variable-specific attributes
  • attrs (dict) – Container attributes
Returns:

container

a Series, DataFrame, or Panel object

Return type:

object

add_coords()[source]
attrs

Coordinates property of a metacsv Container

base_coords
coords

Coordinates property of a metacsv Container

static get_unique_multiindex(series)[source]
static stringify_index_names(series)[source]
static strip_special_attributes(args, kwargs)[source]
to_csv(fp, header_file=None, *args, **kwargs)[source]

Write to a metacsv-formatted csv

Parameters:
  • fp (str) – Path to which to write the metacsv-formatted CSV
  • header_file (str_or_buffer) – A separate metacsv-formatted header file
  • *args

    passed to pandas.to_csv

  • **kwargs

    passed to pandas.to_csv

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> DataFrame(
...     pd.DataFrame(np.random.random((3,4))),
...     attrs={'author': 'my name'}
...     ).to_csv('my-metacsv-data.csv')
to_dataarray()[source]

Convert to an xArray.DataArray

Note

If a DataFrame is passed, columns will be stacked and treated as coordinates. to_dataset is not yet implemented for Panel data.

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> df = DataFrame(
...     np.random.random((3,4)),
...     index=list('ABC'),
...     attrs={'author': 'my name'})
...
>>> df.to_dataarray() 
<xarray.DataArray (ind_0: 3, coldim_0: 4)>
array([[  4.17022005e-01,   7.20324493e-01,   1.14374817e-04,
          3.02332573e-01],
       [  1.46755891e-01,   9.23385948e-02,   1.86260211e-01,
          3.45560727e-01],
       [  3.96767474e-01,   5.38816734e-01,   4.19194514e-01,
          6.85219500e-01]])
Coordinates:
  * ind_0     (ind_0) object 'A' 'B' 'C'
  * coldim_0  (coldim_0) int64 0 1 2 3
Attributes:
    author: my name
to_dataset()[source]

Convert to an xArray.Dataset

Note

If a Series is passed, the variable will be named ‘data’. to_netcdf is not yet implemented for Panel data.

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> df = DataFrame(
...     np.random.random((3,4)),
...     attrs={'author': 'my name'})
...
>>> df.to_dataset()
<xarray.Dataset>
Dimensions:  (index: 3)
Coordinates:
  * index    (index) int64 0 1 2
Data variables:
    0        (index) float64 0.417 0.1468 0.3968
    1        (index) float64 0.7203 0.09234 0.5388
    2        (index) float64 0.0001144 0.1863 0.4192
    3        (index) float64 0.3023 0.3456 0.6852
Attributes:
    author: my name
to_header(fp)[source]

Write attributes directly to a metacsv-formatted header file

fp : str

Path to which to write the metacsv-formatted header file

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> df = DataFrame(
...     np.random.random((3,4)),
...     columns=['col'+str(i) for i in range(4)])
...
>>> df.attrs={'author': 'my name'}
>>> df.to_header('mycsv.header')
to_netcdf(fp)[source]

Convert to a NetCDF file

Note

If a Series is passed, the variable will be named ‘data’. to_netcdf is not yet implemented for Panel data.

Parameters:fp (string_or_buffer) – The filepath or file object to be written

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> df = DataFrame(
...     np.random.random((3,4)),
...     columns=list('ABCD'),
...     attrs={'author': 'my name'})
...
>>> df.to_netcdf('test.nc')
>>> import xarray as xr
>>> xr.open_dataset('test.nc')
<xarray.Dataset>
Dimensions:  (index: 3)
Coordinates:
  * index    (index) int64 0 1 2
Data variables:
    A        (index) float64 0.417 0.1468 0.3968
    B        (index) float64 0.7203 0.09234 0.5388
    C        (index) float64 0.0001144 0.1863 0.4192
    D        (index) float64 0.3023 0.3456 0.6852
Attributes:
    author: my name
to_pandas()[source]

Strip metacsv special attributes and return as a pandas Series, DataFrame, or Panel

Example

>>> from metacsv import DataFrame
>>> import numpy as np, pandas as pd
>>> np.random.seed(1)
>>>
>>> df = DataFrame(
...     np.random.random((3,4)),
...     columns=['col'+str(i) for i in range(4)])
...
>>> df.index = pd.MultiIndex.from_tuples(
...     [('a','X'),('b','Y'),('c','Z')],
...     names=['abc','xyz'])
...
>>> df.attrs={'author': 'my name'}
>>> df.coords = {'abc': None, 'xyz': ['abc']}
>>> df 
<metacsv.core.containers.DataFrame (3, 4)>
             col0      col1      col2      col3
abc xyz
a   X    0.417022  0.720324  0.000114  0.302333
b   Y    0.146756  0.092339  0.186260  0.345561
c   Z    0.396767  0.538817  0.419195  0.685220

Coordinates
  * abc        (abc) object a, b, c
    xyz        (abc) object X, Y, Z
Attributes
    author:         my name

>>> df.to_pandas() 
             col0      col1      col2      col3
abc xyz
a   X    0.417022  0.720324  0.000114  0.302333
b   Y    0.146756  0.092339  0.186260  0.345561
c   Z    0.396767  0.538817  0.419195  0.685220
to_xarray()[source]

Convert to an xArray.Dataset

Note

to_dataset is not yet implemented for Panel data.

Example

>>> from metacsv import DataFrame
>>> import numpy as np
>>> np.random.seed(1)
>>>
>>> df = DataFrame(np.random.random((3,4)), columns=['col'+str(i) for i in range(4)])
>>> df.index = pd.MultiIndex.from_tuples([('a','X'),('b','Y'),('c','Z')], names=['abc','xyz'])
>>> df.attrs={'author': 'my name'}
>>> df.coords = {'abc': None, 'xyz': ['abc']}
>>> df 
<metacsv.core.containers.DataFrame (3, 4)>
             col0      col1      col2      col3
abc xyz
a   X    0.417022  0.720324  0.000114  0.302333
b   Y    0.146756  0.092339  0.186260  0.345561
c   Z    0.396767  0.538817  0.419195  0.685220

Coordinates
  * abc        (abc) object a, b, c
    xyz        (abc) object X, Y, Z
Attributes
    author:         my name

>>> df.to_xarray() 
<xarray.Dataset>
Dimensions:  (abc: 3)
Coordinates:
  * abc      (abc) object 'a' 'b' 'c'
    xyz      (abc) object 'X' 'Y' 'Z'
Data variables:
    col0     (abc) float64 0.417 0.1468 0.3968
    col1     (abc) float64 0.7203 0.09234 0.5388
    col2     (abc) float64 0.0001144 0.1863 0.4192
    col3     (abc) float64 0.3023 0.3456 0.6852
Attributes:
    author: my name
variables

Coordinates property of a metacsv Container

class metacsv.core.internals.Coordinates(coords=None, container=None)[source]

Bases: object

Manages coordinate system for MetaCSV data containers

base_coords
copy()[source]
items()[source]
iteritems()[source]
static parse_coords_definition(coords=None)[source]

Validate coords to test for cyclic graph

property_type = u'Coordinates'
set_coords_from_data()[source]
update(coords=None)[source]
class metacsv.core.internals.Variables(data=None, container=None)[source]

Bases: metacsv.core.internals._BaseProperty

static parse_string_var(defn)[source]
property_type = u'Variables'

Module contents