1- .. doctest-skip-all
2-
31.. _df_narwhals :
42
53Interfacing with DataFrames
@@ -39,26 +37,37 @@ Basic Multi-Backend Example
3937
4038.. EXAMPLE START: Using Generic Multi-Backend Methods
4139
42- Create a table and convert to different DataFrame backends::
40+ Create a table and convert to different DataFrame backends:
4341
4442 >>> from astropy.table import Table
4543 >>> t = Table()
4644 >>> t[' a' ] = [1 , 2 , 3 , 4 ]
4745 >>> t[' b' ] = [' a' , ' b' , ' c' , ' d' ]
4846
49- # Convert to pandas DataFrame
47+ Convert to pandas DataFrame:
48+
49+ .. doctest-requires :: pandas
50+
5051 >>> df_pandas = t.to_df(" pandas" )
5152 >>> type (df_pandas)
5253 <class 'pandas.core.frame.DataFrame'>
5354
54- # Convert to polars DataFrame
55+ Convert to polars DataFrame:
56+
57+ .. doctest-requires :: polars
58+
5559 >>> df_polars = t.to_df(" polars" )
5660 >>> type (df_polars)
5761 <class 'polars.dataframe.frame.DataFrame'>
5862
59- Create a table from any supported DataFrame::
63+ Create a table from any supported DataFrame:
64+
65+ .. doctest-requires :: pandas
6066
6167 >>> t2 = Table.from_df(df_pandas) # From pandas
68+
69+ .. doctest-requires :: polars
70+
6271 >>> t3 = Table.from_df(df_polars) # From polars
6372
6473.. EXAMPLE END
@@ -97,7 +106,9 @@ To demonstrate, we can create a minimal table::
97106 >>> t['a'] = [1, 2, 3, 4]
98107 >>> t['b'] = ['a', 'b', 'c', 'd']
99108
100- Convert to a pandas DataFrame using the pandas-specific method::
109+ Convert to a pandas DataFrame using the pandas-specific method:
110+
111+ .. doctest-requires :: pandas
101112
102113 >>> df = t.to_pandas()
103114 >>> df
@@ -109,7 +120,9 @@ Convert to a pandas DataFrame using the pandas-specific method::
109120 >>> type (df)
110121 <class 'pandas.core.frame.DataFrame'>
111122
112- Create a table from a pandas DataFrame::
123+ Create a table from a pandas DataFrame:
124+
125+ .. doctest-requires :: pandas
113126
114127 >>> t2 = Table.from_pandas(df)
115128 >>> t2
@@ -127,27 +140,35 @@ Create a table from a pandas DataFrame::
127140 Pandas Index Support
128141--------------------
129142
130- The pandas-specific methods provide full support for DataFrame indexing, which is a unique pandas feature::
143+ The pandas-specific methods provide full support for DataFrame indexing, which is a unique pandas feature:
131144
132145 >>> from astropy.time import Time
133146 >>> tm = Time([1998 , 2002 ], format = " jyear" )
134147 >>> x = [1 , 2 ]
135148 >>> t = Table([tm, x], names = [" tm" , " x" ])
136149
137- # Use a column as the DataFrame index
150+ Use a column as the DataFrame index:
151+
152+ .. doctest-requires :: pandas
153+
138154 >>> df = t.to_pandas(index = " tm" )
139155 >>> df.index.name
140156 'tm'
141157
142- # Convert back including the index as a column
158+ Convert back including the index as a column:
159+
160+ .. doctest-requires :: pandas
161+
143162 >>> t_back = Table.from_pandas(df, index = True )
144163 >>> t_back.colnames
145164 ['tm', 'x']
146165
147166Pandas Excel Support
148167--------------------
149168
150- Read an Excel file into a table by utilizing the pandas backend::
169+ Read an Excel file into a table by utilizing the pandas backend:
170+
171+ .. doctest-skip ::
151172
152173 >>> t = Table.from_pandas(pandas.read_excel(" myexceltable.xlsx" ))
153174
@@ -221,7 +242,9 @@ Create a table with masked and mixin columns::
221242 -- 2.0 b 2021-01-02 00:00:00.000 2.0,5.0 2.0
222243 3 -- c 2021-01-03 00:00:00.000 3.0,6.0 3.0
223244
224- Convert using the pandas-specific method::
245+ Convert using the pandas-specific method:
246+
247+ .. doctest-requires :: pandas
225248
226249 >>> df_pandas = t.to_pandas()
227250 >>> df_pandas
@@ -230,12 +253,16 @@ Convert using the pandas-specific method::
230253 1 <NA> 2.0 b 2021-01-02 2.0 5.0 2.0
231254 2 3 NaN c 2021-01-03 3.0 6.0 3.0
232255
233- Convert using the generic method to pandas::
256+ Convert using the generic method to pandas:
257+
258+ .. doctest-requires :: pandas
234259
235260 >>> df_generic = t.to_df(" pandas" )
236261 >>> # Results are identical to df_pandas
237262
238- Convert to polars using the generic method::
263+ Convert to polars using the generic method:
264+
265+ .. doctest-requires :: polars
239266
240267 >>> df_polars = t.to_df(" polars" )
241268 >>> df_polars
@@ -250,10 +277,15 @@ Convert to polars using the generic method::
250277 │ 3 ┆ null ┆ c ┆ 2021-01-03 00:00:00 ┆ 3.0 ┆ 6.0 ┆ 3.0 │
251278 └──────┴──────┴──────┴─────────────────────┴───────┴────────┴─────┘
252279
253- Convert back to tables::
280+ Convert back to tables:
281+
282+ .. doctest-requires :: pandas
254283
255284 >>> t_from_pandas = QTable.from_pandas(df_pandas) # Using pandas-specific method
256285 >>> t_from_generic = QTable.from_df(df_pandas) # Using generic method
286+
287+ .. doctest-requires :: polars
288+
257289 >>> t_from_polars = QTable.from_df(df_polars) # From polars DataFrame
258290
259291Note the data transformations that occurred:
0 commit comments