ntv-pandas.ntv_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"""
 10import pandas as pd
 11from tab_analysis import AnaDataset
 12from ntv_numpy import Xdataset
 13from ntv_pandas.pandas_ntv_connector import to_json, as_def_type, equals
 14from ntv_pandas.pandas_ntv_connector import to_analysis, check_relation
 15
 16try:
 17    # delete the accessor to avoid warning
 18    del pd.DataFrame.npd
 19except AttributeError:
 20    pass
 21
 22
 23@pd.api.extensions.register_dataframe_accessor("npd")
 24class NpdDataFrameAccessor:
 25    """Accessor class for methods invoked as `pd.DataFrame.npd.*`"""
 26
 27    def __init__(self, pandas_obj):
 28        self._obj = pandas_obj
 29
 30    def analysis(self, distr=False):
 31        """Accessor for method `tab_analysis.AnaDataset` applied with
 32        `pandas_ntv_connector.to_analysis` invoked as `pd.DataFrame.npd.analysis`"""
 33        return AnaDataset(to_analysis(self._obj, distr))
 34
 35    def check_relation(self, parent, child, typecoupl, value=True):
 36        ''' Accessor for method `pandas_ntv_connector.check_relation` invoket as
 37        `pd.DataFrame.npd.check_relation`'''
 38        return check_relation(self._obj, parent, child, typecoupl, value)
 39
 40    def to_json(self, **kwargs):
 41        """Accessor for method `pandas_ntv_connector.to_json` invoked as
 42        `pd.DataFrame.npd.to_json`
 43
 44        *parameters*
 45
 46        - **pd_array** : Series or Dataframe to convert
 47        - **encoded** : boolean (default: False) - if True return a JSON text else a JSON value
 48        - **header** : boolean (default: True) - if True the JSON data is included as
 49        value in a {key:value} object where key is ':field' for Series or ':tab' for DataFrame
 50        - **table** : boolean (default False) - if True return TableSchema format
 51        - **index** : boolean (default True) - if True the index Series is included"""
 52        return to_json(self._obj, **kwargs)
 53
 54    def as_def_type(self):
 55        """Accessor for method `pandas_ntv_connector.as_def_type` invoked as
 56        `pd.DataFrame.npd.as_def_type`"""
 57        return as_def_type(self._obj)
 58
 59    def equals(self, other):
 60        """Accessor for method `pandas_ntv_connector.equals` invoked as
 61        `pd.DataFrame.npd.equals`"""
 62        return equals(self._obj, other)
 63
 64    def to_xarray(self, **kwargs):
 65        """Accessor for method `Xdataset.from_dataframe.to_xarray` invoked as
 66        `pd.DataFrame.npd.to_xarray`.
 67
 68        *Parameters*
 69
 70        - **dims**: list of string (default None) - order of dimensions to apply
 71        - **dataset**: Boolean (default True) - if False and a single data_var,
 72        return a sc.DataArray
 73        - **datagroup**: Boolean (default True) - if True, return a sc.DataGroup
 74        which contains the sc.DataArray/sc.Dataset and the other data else only
 75        sc.DataArray/sc.Dataset"""
 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        - **datagroup**: 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)
@pd.api.extensions.register_dataframe_accessor('npd')
class NpdDataFrameAccessor:
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 sc.DataArray
74        - **datagroup**: Boolean (default True) - if True, return a sc.DataGroup
75        which contains the sc.DataArray/sc.Dataset and the other data else only
76        sc.DataArray/sc.Dataset"""
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        - **datagroup**: 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.*

NpdDataFrameAccessor(pandas_obj)
28    def __init__(self, pandas_obj):
29        self._obj = pandas_obj
def analysis(self, distr=False):
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))

Accessor for method tab_analysis.AnaDataset applied with pandas_ntv_connector.to_analysis invoked as pd.DataFrame.npd.analysis

def check_relation(self, parent, child, typecoupl, value=True):
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)

Accessor for method pandas_ntv_connector.check_relation invoket as pd.DataFrame.npd.check_relation

def to_json(self, **kwargs):
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)

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
def as_def_type(self):
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)

Accessor for method pandas_ntv_connector.as_def_type invoked as pd.DataFrame.npd.as_def_type

def equals(self, other):
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)

Accessor for method pandas_ntv_connector.equals invoked as pd.DataFrame.npd.equals

def to_xarray(self, **kwargs):
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 sc.DataArray
74        - **datagroup**: Boolean (default True) - if True, return a sc.DataGroup
75        which contains the sc.DataArray/sc.Dataset and the other data else only
76        sc.DataArray/sc.Dataset"""
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 sc.DataArray
  • datagroup: Boolean (default True) - if True, return a sc.DataGroup which contains the sc.DataArray/sc.Dataset and the other data else only sc.DataArray/sc.Dataset
def to_scipp(self, **kwargs):
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        - **datagroup**: 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
  • datagroup: 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
@pd.api.extensions.register_series_accessor('npd')
class NpdSeriesAccessor:
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.*

NpdSeriesAccessor(pandas_obj)
105    def __init__(self, pandas_obj):
106        self._obj = pandas_obj
def to_json(self, **kwargs):
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

def as_def_type(self):
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)

Accessor for method pandas_ntv_connector.as_def_type invoked as pd.Series.npd.as_def_type

def equals(self, other):
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 for method pandas_ntv_connector.equals invoked as pd.DataFrame.npd.equals