ntv-pandas.ntv_pandas.pandas_accessors
Created on Sun Oct 8 2023
@author: philippe@loco-labs.io
Accessor methods bound to pd.Series.npd, pd.DataFrame.npd
1# -*- coding: utf-8 -*- 2""" 3Created on Sun Oct 8 2023 4 5@author: philippe@loco-labs.io 6 7 8Accessor methods bound to pd.Series.npd, pd.DataFrame.npd 9""" 10 11import pandas as pd 12from tab_analysis import AnaDataset 13from ntv_numpy import Xdataset 14from ntv_pandas.pandas_ntv_connector import to_json, as_def_type, equals 15from ntv_pandas.pandas_ntv_connector import to_analysis, check_relation 16 17try: 18 # delete the accessor to avoid warning 19 del pd.DataFrame.npd 20except AttributeError: 21 pass 22 23 24@pd.api.extensions.register_dataframe_accessor("npd") 25class NpdDataFrameAccessor: 26 """Accessor class for methods invoked as `pd.DataFrame.npd.*`""" 27 28 def __init__(self, pandas_obj): 29 self._obj = pandas_obj 30 31 def analysis(self, distr=False): 32 """Accessor for method `tab_analysis.AnaDataset` applied with 33 `pandas_ntv_connector.to_analysis` invoked as `pd.DataFrame.npd.analysis`""" 34 return AnaDataset(to_analysis(self._obj, distr)) 35 36 def check_relation(self, parent, child, typecoupl, value=True): 37 """Accessor for method `pandas_ntv_connector.check_relation` invoket as 38 `pd.DataFrame.npd.check_relation`""" 39 return check_relation(self._obj, parent, child, typecoupl, value) 40 41 def to_json(self, **kwargs): 42 """Accessor for method `pandas_ntv_connector.to_json` invoked as 43 `pd.DataFrame.npd.to_json` 44 45 *parameters* 46 47 - **pd_array** : Series or Dataframe to convert 48 - **encoded** : boolean (default: False) - if True return a JSON text else a JSON value 49 - **header** : boolean (default: True) - if True the JSON data is included as 50 value in a {key:value} object where key is ':field' for Series or ':tab' for DataFrame 51 - **table** : boolean (default False) - if True return TableSchema format 52 - **index** : boolean (default True) - if True the index Series is included""" 53 return to_json(self._obj, **kwargs) 54 55 def as_def_type(self): 56 """Accessor for method `pandas_ntv_connector.as_def_type` invoked as 57 `pd.DataFrame.npd.as_def_type`""" 58 return as_def_type(self._obj) 59 60 def equals(self, other): 61 """Accessor for method `pandas_ntv_connector.equals` invoked as 62 `pd.DataFrame.npd.equals`""" 63 return equals(self._obj, other) 64 65 def to_xarray(self, **kwargs): 66 """Accessor for method `Xdataset.from_dataframe.to_xarray` invoked as 67 `pd.DataFrame.npd.to_xarray`. 68 69 *Parameters* 70 71 - **dims**: list of string (default None) - order of dimensions to apply 72 - **dataset** : Boolean (default True) - if False and a single data_var, 73 return a xr.DataArray 74 - **info** : Boolean (default True) - if True, add json representation 75 of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs""" 76 return Xdataset.from_dataframe(self._obj, **kwargs).to_xarray(**kwargs) 77 78 def to_scipp(self, **kwargs): 79 """Accessor for method `Xdataset.from_dataframe.to_scipp` invoked as 80 `pd.DataFrame.npd.to_scipp`. 81 82 *Parameters* 83 84 - **dims**: list of string (default None) - order of dimensions to apply 85 - **dataset**: Boolean (default True) - if False and a single data_var, 86 return a DataArray 87 - **info**: Boolean (default True) - if True return a DataGroup with 88 metadata and data_arrays 89 - **ntv_type**: Boolean (default True) - if True add ntv-type to the name""" 90 return Xdataset.from_dataframe(self._obj, **kwargs).to_scipp(**kwargs) 91 92 93try: 94 # delete the accessor to avoid warning 95 del pd.Series.npd 96except AttributeError: 97 pass 98 99 100@pd.api.extensions.register_series_accessor("npd") 101class NpdSeriesAccessor: 102 """Accessor class for methods invoked as `pd.Series.npd.*`""" 103 104 def __init__(self, pandas_obj): 105 self._obj = pandas_obj 106 107 def to_json(self, **kwargs): 108 """Accessor for method `pandas_ntv_connector.to_json` invoked as 109 `pd.Series.npd.to_json`""" 110 return to_json(self._obj, **kwargs) 111 112 def as_def_type(self): 113 """Accessor for method `pandas_ntv_connector.as_def_type` invoked as 114 `pd.Series.npd.as_def_type`""" 115 return as_def_type(self._obj) 116 117 def equals(self, other): 118 """Accessor for method `pandas_ntv_connector.equals` invoked as 119 `pd.DataFrame.npd.equals`""" 120 return equals(self._obj, other)
25@pd.api.extensions.register_dataframe_accessor("npd") 26class NpdDataFrameAccessor: 27 """Accessor class for methods invoked as `pd.DataFrame.npd.*`""" 28 29 def __init__(self, pandas_obj): 30 self._obj = pandas_obj 31 32 def analysis(self, distr=False): 33 """Accessor for method `tab_analysis.AnaDataset` applied with 34 `pandas_ntv_connector.to_analysis` invoked as `pd.DataFrame.npd.analysis`""" 35 return AnaDataset(to_analysis(self._obj, distr)) 36 37 def check_relation(self, parent, child, typecoupl, value=True): 38 """Accessor for method `pandas_ntv_connector.check_relation` invoket as 39 `pd.DataFrame.npd.check_relation`""" 40 return check_relation(self._obj, parent, child, typecoupl, value) 41 42 def to_json(self, **kwargs): 43 """Accessor for method `pandas_ntv_connector.to_json` invoked as 44 `pd.DataFrame.npd.to_json` 45 46 *parameters* 47 48 - **pd_array** : Series or Dataframe to convert 49 - **encoded** : boolean (default: False) - if True return a JSON text else a JSON value 50 - **header** : boolean (default: True) - if True the JSON data is included as 51 value in a {key:value} object where key is ':field' for Series or ':tab' for DataFrame 52 - **table** : boolean (default False) - if True return TableSchema format 53 - **index** : boolean (default True) - if True the index Series is included""" 54 return to_json(self._obj, **kwargs) 55 56 def as_def_type(self): 57 """Accessor for method `pandas_ntv_connector.as_def_type` invoked as 58 `pd.DataFrame.npd.as_def_type`""" 59 return as_def_type(self._obj) 60 61 def equals(self, other): 62 """Accessor for method `pandas_ntv_connector.equals` invoked as 63 `pd.DataFrame.npd.equals`""" 64 return equals(self._obj, other) 65 66 def to_xarray(self, **kwargs): 67 """Accessor for method `Xdataset.from_dataframe.to_xarray` invoked as 68 `pd.DataFrame.npd.to_xarray`. 69 70 *Parameters* 71 72 - **dims**: list of string (default None) - order of dimensions to apply 73 - **dataset** : Boolean (default True) - if False and a single data_var, 74 return a xr.DataArray 75 - **info** : Boolean (default True) - if True, add json representation 76 of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs""" 77 return Xdataset.from_dataframe(self._obj, **kwargs).to_xarray(**kwargs) 78 79 def to_scipp(self, **kwargs): 80 """Accessor for method `Xdataset.from_dataframe.to_scipp` invoked as 81 `pd.DataFrame.npd.to_scipp`. 82 83 *Parameters* 84 85 - **dims**: list of string (default None) - order of dimensions to apply 86 - **dataset**: Boolean (default True) - if False and a single data_var, 87 return a DataArray 88 - **info**: Boolean (default True) - if True return a DataGroup with 89 metadata and data_arrays 90 - **ntv_type**: Boolean (default True) - if True add ntv-type to the name""" 91 return Xdataset.from_dataframe(self._obj, **kwargs).to_scipp(**kwargs)
Accessor class for methods invoked as pd.DataFrame.npd.*
32 def analysis(self, distr=False): 33 """Accessor for method `tab_analysis.AnaDataset` applied with 34 `pandas_ntv_connector.to_analysis` invoked as `pd.DataFrame.npd.analysis`""" 35 return AnaDataset(to_analysis(self._obj, distr))
Accessor for method tab_analysis.AnaDataset
applied with
pandas_ntv_connector.to_analysis
invoked as pd.DataFrame.npd.analysis
37 def check_relation(self, parent, child, typecoupl, value=True): 38 """Accessor for method `pandas_ntv_connector.check_relation` invoket as 39 `pd.DataFrame.npd.check_relation`""" 40 return check_relation(self._obj, parent, child, typecoupl, value)
Accessor for method pandas_ntv_connector.check_relation
invoket as
pd.DataFrame.npd.check_relation
42 def to_json(self, **kwargs): 43 """Accessor for method `pandas_ntv_connector.to_json` invoked as 44 `pd.DataFrame.npd.to_json` 45 46 *parameters* 47 48 - **pd_array** : Series or Dataframe to convert 49 - **encoded** : boolean (default: False) - if True return a JSON text else a JSON value 50 - **header** : boolean (default: True) - if True the JSON data is included as 51 value in a {key:value} object where key is ':field' for Series or ':tab' for DataFrame 52 - **table** : boolean (default False) - if True return TableSchema format 53 - **index** : boolean (default True) - if True the index Series is included""" 54 return to_json(self._obj, **kwargs)
Accessor for method pandas_ntv_connector.to_json
invoked as
pd.DataFrame.npd.to_json
parameters
- pd_array : Series or Dataframe to convert
- encoded : boolean (default: False) - if True return a JSON text else a JSON value
- header : boolean (default: True) - if True the JSON data is included as value in a {key:value} object where key is ':field' for Series or ':tab' for DataFrame
- table : boolean (default False) - if True return TableSchema format
- index : boolean (default True) - if True the index Series is included
56 def as_def_type(self): 57 """Accessor for method `pandas_ntv_connector.as_def_type` invoked as 58 `pd.DataFrame.npd.as_def_type`""" 59 return as_def_type(self._obj)
Accessor for method pandas_ntv_connector.as_def_type
invoked as
pd.DataFrame.npd.as_def_type
61 def equals(self, other): 62 """Accessor for method `pandas_ntv_connector.equals` invoked as 63 `pd.DataFrame.npd.equals`""" 64 return equals(self._obj, other)
Accessor for method pandas_ntv_connector.equals
invoked as
pd.DataFrame.npd.equals
66 def to_xarray(self, **kwargs): 67 """Accessor for method `Xdataset.from_dataframe.to_xarray` invoked as 68 `pd.DataFrame.npd.to_xarray`. 69 70 *Parameters* 71 72 - **dims**: list of string (default None) - order of dimensions to apply 73 - **dataset** : Boolean (default True) - if False and a single data_var, 74 return a xr.DataArray 75 - **info** : Boolean (default True) - if True, add json representation 76 of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs""" 77 return Xdataset.from_dataframe(self._obj, **kwargs).to_xarray(**kwargs)
Accessor for method Xdataset.from_dataframe.to_xarray
invoked as
pd.DataFrame.npd.to_xarray
.
Parameters
- dims: list of string (default None) - order of dimensions to apply
- dataset : Boolean (default True) - if False and a single data_var, return a xr.DataArray
- info : Boolean (default True) - if True, add json representation of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs
79 def to_scipp(self, **kwargs): 80 """Accessor for method `Xdataset.from_dataframe.to_scipp` invoked as 81 `pd.DataFrame.npd.to_scipp`. 82 83 *Parameters* 84 85 - **dims**: list of string (default None) - order of dimensions to apply 86 - **dataset**: Boolean (default True) - if False and a single data_var, 87 return a DataArray 88 - **info**: Boolean (default True) - if True return a DataGroup with 89 metadata and data_arrays 90 - **ntv_type**: Boolean (default True) - if True add ntv-type to the name""" 91 return Xdataset.from_dataframe(self._obj, **kwargs).to_scipp(**kwargs)
Accessor for method Xdataset.from_dataframe.to_scipp
invoked as
pd.DataFrame.npd.to_scipp
.
Parameters
- dims: list of string (default None) - order of dimensions to apply
- dataset: Boolean (default True) - if False and a single data_var, return a DataArray
- info: Boolean (default True) - if True return a DataGroup with metadata and data_arrays
- ntv_type: Boolean (default True) - if True add ntv-type to the name
101@pd.api.extensions.register_series_accessor("npd") 102class NpdSeriesAccessor: 103 """Accessor class for methods invoked as `pd.Series.npd.*`""" 104 105 def __init__(self, pandas_obj): 106 self._obj = pandas_obj 107 108 def to_json(self, **kwargs): 109 """Accessor for method `pandas_ntv_connector.to_json` invoked as 110 `pd.Series.npd.to_json`""" 111 return to_json(self._obj, **kwargs) 112 113 def as_def_type(self): 114 """Accessor for method `pandas_ntv_connector.as_def_type` invoked as 115 `pd.Series.npd.as_def_type`""" 116 return as_def_type(self._obj) 117 118 def equals(self, other): 119 """Accessor for method `pandas_ntv_connector.equals` invoked as 120 `pd.DataFrame.npd.equals`""" 121 return equals(self._obj, other)
Accessor class for methods invoked as pd.Series.npd.*
108 def to_json(self, **kwargs): 109 """Accessor for method `pandas_ntv_connector.to_json` invoked as 110 `pd.Series.npd.to_json`""" 111 return to_json(self._obj, **kwargs)
Accessor for method pandas_ntv_connector.to_json
invoked as
pd.Series.npd.to_json