@@ -278,7 +278,7 @@ def add_nested(
278278 Examples
279279 --------
280280
281-
281+ >>> import nested_pandas as npd
282282 >>> nf = npd.NestedFrame({"a": [1, 2, 3], "b": [4, 5, 6]},
283283 ... index=[0,1,2])
284284 >>> 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:
320320 Examples
321321 --------
322322
323+ >>> import nested_pandas as npd
323324 >>> nf = npd.NestedFrame({"c":[1,2,3], "d":[2,4,6],
324325 ... "e":[[1,2,3], [4,5,6], [7,8,9]]},
325326 ... index=[0,1,2])
326327
327- >>> nf.nest_lists(columns=["c","d "], name="nested")
328+ >>> nf.nest_lists(columns=["e "], name="nested")
328329 c d nested
329330 0 1 2 [{e: 1}; …] (3 rows)
330331 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,
367368 Examples
368369 --------
369370
371+ >>> import nested_pandas as npd
370372 >>> nf = npd.NestedFrame({"a":[1,1,1,2,2], "b":[2,2,2,4,4],
371373 ... "c":[1,2,3,4,5], "d":[2,4,6,8,10]},
372374 ... index=[0,0,0,1,1])
@@ -424,6 +426,7 @@ def from_lists(cls, df, base_columns=None, list_columns=None, name="nested"):
424426 Examples
425427 --------
426428
429+ >>> import nested_pandas as npd
427430 >>> nf = npd.NestedFrame({"c":[1,2,3], "d":[2,4,6],
428431 ... "e":[[1,2,3], [4,5,6], [7,8,9]]},
429432 ... index=[0,1,2])
@@ -605,7 +608,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> NestedFrame |
605608
606609 >>> nf = nf.query("nested.t > 10")
607610 >>> nf
608- a b nested
611+ a b nested
609612 0 0.417022 0.184677 [{t: 13.40935, flux: 98.886109, band: 'g'}; …]...
610613 1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
611614 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 |
619622 with rows of that particular nested structure filtered. For example,
620623 querying the NestedFrame "df" with nested structure "my_nested" as
621624 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")`
625626 """
626627 if not isinstance (expr , str ):
627628 msg = f"expr must be a string to be evaluated, { type (expr )} given"
@@ -786,7 +787,7 @@ def dropna(
786787 >>> # this query empties several of the nested dataframes
787788 >>> nf = nf.query("nested.t > 19")
788789 >>> nf
789- a b nested
790+ a b nested
790791 0 0.417022 0.184677 None
791792 1 0.720324 0.372520 [{t: 19.365232, flux: 90.85955, band: 'r'}]
792793 2 0.000114 0.691121 [{t: 19.157791, flux: 14.672857, band: 'r'}]
@@ -796,7 +797,7 @@ def dropna(
796797
797798 >>> # dropna removes rows with those emptied dataframes
798799 >>> nf.dropna(subset="nested")
799- a b nested
800+ a b nested
800801 1 0.720324 0.372520 [{t: 19.365232, flux: 90.85955, band: 'r'}]
801802 2 0.000114 0.691121 [{t: 19.157791, flux: 14.672857, band: 'r'}]
802803
@@ -806,9 +807,20 @@ def dropna(
806807 >>> nf = generate_data(5,5, seed=1)
807808 >>> # Either on the whole dataframe
808809 >>> 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'}; …]...
809816 >>> # or on a specific nested column
810817 >>> 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'}; …]...
812824
813825 Notes
814826 -----
@@ -909,7 +921,7 @@ def sort_values(
909921
910922 >>> # Sort nested values
911923 >>> nf.sort_values(by="nested.band")
912- a b nested
924+ a b nested
913925 0 0.417022 0.184677 [{t: 13.40935, flux: 98.886109, band: 'g'}; …]...
914926 1 0.720324 0.372520 [{t: 13.70439, flux: 68.650093, band: 'g'}; …]...
915927 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
10171029 >>> from nested_pandas.datasets.generation import generate_data
10181030 >>> import numpy as np
10191031 >>> nf = generate_data(5,5, seed=1)
1020-
1032+ >>>
10211033 >>> # define a custom user function
1034+ >>> # reduce will return a NestedFrame with two columns
10221035 >>> 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+ >>>
10271041 >>> # apply the function
10281042 >>> nf.reduce(example_func, "a", "nested.t")
10291043 mean mean_minus_base
@@ -1038,8 +1052,8 @@ def reduce(self, func, *args, infer_nesting=True, **kwargs) -> NestedFrame: # t
10381052
10391053 >>> # define a custom user function that returns nested structure
10401054 >>> 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,
10431057 ... "offsets.t_b": nested_col - base_col2}
10441058
10451059 By giving both output columns the prefix "offsets.", we signal
0 commit comments