11# Pandas accessors
22
3- Pandas-OpenSCM also provides a [ pandas] [ ] accessor .
3+ Pandas-OpenSCM also provides [ pandas] [ ] accessors .
44For details of the implementation of this pattern, see
55[ pandas' docs] ( https://pandas.pydata.org/docs/development/extending.html#registering-custom-accessors ) .
66
@@ -10,8 +10,7 @@ which is a pattern we have had bad experiences with in the past).
1010This is done with
1111[ register_pandas_accessors] [ pandas_openscm.accessors.register_pandas_accessors ] ,
1212
13- By default, the accessors are provided under the "openscm" namespace
14- and this is how the accessors are documented below.
13+ By default, the accessors are provided under the "openscm" namespace.
1514However, the namespace can be customised when using
1615[ register_pandas_accessors] [ pandas_openscm.accessors.register_pandas_accessors ] ,
1716should you wish to use a different namespace for the accessor.
@@ -22,29 +21,41 @@ you will need to run something like:
2221``` python
2322from pandas_openscm.accessors import register_pandas_accessors
2423
25- # The 'pd.DataFrame.openscm' and 'pd.Series.openscm' namespace
24+ # The 'pd.DataFrame.openscm' and 'pd.Series.openscm' namespaces
2625# will not be available at this point.
2726
2827# Register the accessors
2928register_pandas_accessors()
3029
31- # The 'pd.DataFrame.openscm' and 'pd.Series.openscm' namespace
32- # (or whatever other custom namespace you chose to register)
30+ # The 'pd.DataFrame.openscm' and 'pd.Series.openscm' namespaces
3331# will now be available.
32+ # I.e. you could now do something like
33+ df = pd.DataFrame(
34+ [
35+ [1.1 , 0.8 , 1.2 ],
36+ [2.1 , np.nan, 8.4 ],
37+ ],
38+ columns = [2010.0 , 2015.0 , 2025.0 ],
39+ index = pd.MultiIndex.from_tuples(
40+ [
41+ (" sa" , " v2" , " W" ),
42+ (" sb" , " v2" , " W" ),
43+ ],
44+ names = [" scenario" , " variable" , " unit" ],
45+ ),
46+ )
47+
48+ # Use pandas-openscm's functionality via the registered accessors.
49+ df.openscm.to_long_data()
50+
51+ # If you want to register the accessors under a custom namespace instead,
52+ # use something like the below instead
53+ register_pandas_accessors(namespace = " my_custom_namespace" )
54+
55+ # Doing it this way will make the custom namespace available under
56+ # 'pd.DataFrame.my_custom_namespace' and 'pd.Series.my_custom_namespace'.
3457```
3558
36- The full accessor API is documented below.
37-
38- ::: pandas_openscm.accessors.dataframe.PandasDataFrameOpenSCMAccessor
39- handler: python_accessors
40- options:
41- namespace: "pd.DataFrame.openscm"
42- show_root_full_path: false
43- show_root_heading: true
44-
45- ::: pandas_openscm.accessors.series.PandasSeriesOpenSCMAccessor
46- handler: python_accessors
47- options:
48- namespace: "pd.Series.openscm"
49- show_root_full_path: false
50- show_root_heading: true
59+ The full accessor APIs are documented at
60+ [ pandas_openscm.accessors.dataframe.PandasDataFrameOpenSCMAccessor] [ ]
61+ and [ pandas_openscm.accessors.series.PandasSeriesOpenSCMAccessor] [ ] .
0 commit comments