@@ -278,7 +278,7 @@ def add_nested(
278
278
Examples
279
279
--------
280
280
281
-
281
+ >>> import nested_pandas as npd
282
282
>>> nf = npd.NestedFrame({"a": [1, 2, 3], "b": [4, 5, 6]},
283
283
... index=[0,1,2])
284
284
>>> nf2 = npd.NestedFrame({"c":[1,2,3,4,5,6,7,8,9]},
@@ -320,11 +320,12 @@ def nest_lists(self, name: str, columns: list[str]) -> NestedFrame:
320
320
Examples
321
321
--------
322
322
323
+ >>> import nested_pandas as npd
323
324
>>> nf = npd.NestedFrame({"c":[1,2,3], "d":[2,4,6],
324
325
... "e":[[1,2,3], [4,5,6], [7,8,9]]},
325
326
... index=[0,1,2])
326
327
327
- >>> nf.nest_lists(columns=["c","d "], name="nested")
328
+ >>> nf.nest_lists(columns=["e "], name="nested")
328
329
c d nested
329
330
0 1 2 [{e: 1}; …] (3 rows)
330
331
1 2 4 [{e: 4}; …] (3 rows)
@@ -367,6 +368,7 @@ def from_flat(cls, df, base_columns, nested_columns=None, on: str | None = None,
367
368
Examples
368
369
--------
369
370
371
+ >>> import nested_pandas as npd
370
372
>>> nf = npd.NestedFrame({"a":[1,1,1,2,2], "b":[2,2,2,4,4],
371
373
... "c":[1,2,3,4,5], "d":[2,4,6,8,10]},
372
374
... index=[0,0,0,1,1])
@@ -424,6 +426,7 @@ def from_lists(cls, df, base_columns=None, list_columns=None, name="nested"):
424
426
Examples
425
427
--------
426
428
429
+ >>> import nested_pandas as npd
427
430
>>> nf = npd.NestedFrame({"c":[1,2,3], "d":[2,4,6],
428
431
... "e":[[1,2,3], [4,5,6], [7,8,9]]},
429
432
... index=[0,1,2])
@@ -605,7 +608,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> NestedFrame |
605
608
606
609
>>> nf = nf.query("nested.t > 10")
607
610
>>> nf
608
- a b nested
611
+ a b nested
609
612
0 0.417022 0.184677 [{t: 13.40935, flux: 98.886109, band: 'g'}; …]...
610
613
1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
611
614
2 0.000114 0.691121 [{t: 11.173797, flux: 28.044399, band: 'r'}; …...
@@ -619,9 +622,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> NestedFrame |
619
622
with rows of that particular nested structure filtered. For example,
620
623
querying the NestedFrame "df" with nested structure "my_nested" as
621
624
below will return all rows of df, but with mynested filtered by the
622
- condition:
623
-
624
- >>> df.query("mynested.a > 2")
625
+ condition: `nf.query("mynested.a > 2")`
625
626
"""
626
627
if not isinstance (expr , str ):
627
628
msg = f"expr must be a string to be evaluated, { type (expr )} given"
@@ -786,7 +787,7 @@ def dropna(
786
787
>>> # this query empties several of the nested dataframes
787
788
>>> nf = nf.query("nested.t > 19")
788
789
>>> nf
789
- a b nested
790
+ a b nested
790
791
0 0.417022 0.184677 None
791
792
1 0.720324 0.372520 [{t: 19.365232, flux: 90.85955, band: 'r'}]
792
793
2 0.000114 0.691121 [{t: 19.157791, flux: 14.672857, band: 'r'}]
@@ -796,7 +797,7 @@ def dropna(
796
797
797
798
>>> # dropna removes rows with those emptied dataframes
798
799
>>> nf.dropna(subset="nested")
799
- a b nested
800
+ a b nested
800
801
1 0.720324 0.372520 [{t: 19.365232, flux: 90.85955, band: 'r'}]
801
802
2 0.000114 0.691121 [{t: 19.157791, flux: 14.672857, band: 'r'}]
802
803
@@ -806,9 +807,20 @@ def dropna(
806
807
>>> nf = generate_data(5,5, seed=1)
807
808
>>> # Either on the whole dataframe
808
809
>>> nf.dropna(on_nested="nested")
810
+ a b nested
811
+ 0 0.417022 0.184677 [{t: 8.38389, flux: 31.551563, band: 'r'}; …] ...
812
+ 1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
813
+ 2 0.000114 0.691121 [{t: 4.089045, flux: 83.462567, band: 'g'}; …]...
814
+ 3 0.302333 0.793535 [{t: 17.562349, flux: 1.828828, band: 'g'}; …]...
815
+ 4 0.146756 1.077633 [{t: 0.547752, flux: 75.014431, band: 'g'}; …]...
809
816
>>> # or on a specific nested column
810
817
>>> nf.dropna(subset="nested.t")
811
-
818
+ a b nested
819
+ 0 0.417022 0.184677 [{t: 8.38389, flux: 31.551563, band: 'r'}; …] ...
820
+ 1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
821
+ 2 0.000114 0.691121 [{t: 4.089045, flux: 83.462567, band: 'g'}; …]...
822
+ 3 0.302333 0.793535 [{t: 17.562349, flux: 1.828828, band: 'g'}; …]...
823
+ 4 0.146756 1.077633 [{t: 0.547752, flux: 75.014431, band: 'g'}; …]...
812
824
813
825
Notes
814
826
-----
@@ -909,7 +921,7 @@ def sort_values(
909
921
910
922
>>> # Sort nested values
911
923
>>> nf.sort_values(by="nested.band")
912
- a b nested
924
+ a b nested
913
925
0 0.417022 0.184677 [{t: 13.40935, flux: 98.886109, band: 'g'}; …]...
914
926
1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
915
927
2 0.000114 0.691121 [{t: 4.089045, flux: 83.462567, band: 'g'}; …]...
@@ -1017,13 +1029,15 @@ def reduce(self, func, *args, infer_nesting=True, **kwargs) -> NestedFrame: # t
1017
1029
>>> from nested_pandas.datasets.generation import generate_data
1018
1030
>>> import numpy as np
1019
1031
>>> nf = generate_data(5,5, seed=1)
1020
-
1032
+ >>>
1021
1033
>>> # define a custom user function
1034
+ >>> # reduce will return a NestedFrame with two columns
1022
1035
>>> def example_func(base_col, nested_col):
1023
- >>> '''reduce will return a NestedFrame with two columns'''
1024
- >>> return {"mean": np.mean(nested_col),
1025
- ... "mean_minus_base": np.mean(nested_col) - base_col}
1026
-
1036
+ ... return {
1037
+ ... "mean": np.mean(nested_col),
1038
+ ... "mean_minus_base": np.mean(nested_col) - base_col,
1039
+ ... }
1040
+ >>>
1027
1041
>>> # apply the function
1028
1042
>>> nf.reduce(example_func, "a", "nested.t")
1029
1043
mean mean_minus_base
@@ -1038,8 +1052,8 @@ def reduce(self, func, *args, infer_nesting=True, **kwargs) -> NestedFrame: # t
1038
1052
1039
1053
>>> # define a custom user function that returns nested structure
1040
1054
>>> def example_func(base_col1, base_col2, nested_col):
1041
- >>> '''reduce will return a NestedFrame with nested structure'''
1042
- >>> return {"offsets.t_a": nested_col - base_col1,
1055
+ ... '''reduce will return a NestedFrame with nested structure'''
1056
+ ... return {"offsets.t_a": nested_col - base_col1,
1043
1057
... "offsets.t_b": nested_col - base_col2}
1044
1058
1045
1059
By giving both output columns the prefix "offsets.", we signal
0 commit comments