1- .. doctest-skip-all
2-
31.. _df_narwhals :
42
53Interfacing with DataFrames
@@ -39,26 +37,40 @@ 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" )
52+
53+ .. doctest-requires :: pandas>=3.0
54+
5155 >>> type (df_pandas)
52- <class 'pandas.core.frame.DataFrame'>
56+ <class 'pandas.DataFrame'>
57+
58+ Convert to polars DataFrame:
59+
60+ .. doctest-requires :: polars
5361
54- # Convert to polars DataFrame
5562 >>> df_polars = t.to_df(" polars" )
5663 >>> type (df_polars)
5764 <class 'polars.dataframe.frame.DataFrame'>
5865
59- Create a table from any supported DataFrame::
66+ Create a table from any supported DataFrame:
67+
68+ .. doctest-requires :: pandas
6069
6170 >>> t2 = Table.from_df(df_pandas) # From pandas
71+
72+ .. doctest-requires :: polars
73+
6274 >>> t3 = Table.from_df(df_polars) # From polars
6375
6476.. EXAMPLE END
@@ -97,7 +109,9 @@ To demonstrate, we can create a minimal table::
97109 >>> t['a'] = [1, 2, 3, 4]
98110 >>> t['b'] = ['a', 'b', 'c', 'd']
99111
100- Convert to a pandas DataFrame using the pandas-specific method::
112+ Convert to a pandas DataFrame using the pandas-specific method:
113+
114+ .. doctest-requires :: pandas
101115
102116 >>> df = t.to_pandas()
103117 >>> df
@@ -106,48 +120,61 @@ Convert to a pandas DataFrame using the pandas-specific method::
106120 1 2 b
107121 2 3 c
108122 3 4 d
123+
124+ .. doctest-requires :: pandas>=3.0
125+
109126 >>> type (df)
110- <class 'pandas.core.frame. DataFrame'>
127+ <class 'pandas.DataFrame'>
111128
112- Create a table from a pandas DataFrame::
129+ Create a table from a pandas DataFrame:
130+
131+ .. doctest-requires :: pandas
113132
114133 >>> t2 = Table.from_pandas(df)
115134 >>> t2
116135 <Table length=4>
117- a b
118- int64 string8
119- ----- -------
120- 1 a
121- 2 b
122- 3 c
123- 4 d
136+ a b
137+ int64 str1
138+ ----- ----
139+ 1 a
140+ 2 b
141+ 3 c
142+ 4 d
124143
125144.. EXAMPLE END
126145
127146 Pandas Index Support
128147--------------------
129148
130- The pandas-specific methods provide full support for DataFrame indexing, which is a unique pandas feature::
149+ The pandas-specific methods provide full support for DataFrame indexing, which is a unique pandas feature:
131150
132151 >>> from astropy.time import Time
133152 >>> tm = Time([1998 , 2002 ], format = " jyear" )
134153 >>> x = [1 , 2 ]
135154 >>> t = Table([tm, x], names = [" tm" , " x" ])
136155
137- # Use a column as the DataFrame index
156+ Use a column as the DataFrame index:
157+
158+ .. doctest-requires :: pandas
159+
138160 >>> df = t.to_pandas(index = " tm" )
139161 >>> df.index.name
140162 'tm'
141163
142- # Convert back including the index as a column
164+ Convert back including the index as a column:
165+
166+ .. doctest-requires :: pandas
167+
143168 >>> t_back = Table.from_pandas(df, index = True )
144169 >>> t_back.colnames
145170 ['tm', 'x']
146171
147172Pandas Excel Support
148173--------------------
149174
150- Read an Excel file into a table by utilizing the pandas backend::
175+ Read an Excel file into a table by utilizing the pandas backend:
176+
177+ .. doctest-skip ::
151178
152179 >>> t = Table.from_pandas(pandas.read_excel(" myexceltable.xlsx" ))
153180
@@ -221,7 +248,9 @@ Create a table with masked and mixin columns::
221248 -- 2.0 b 2021-01-02 00:00:00.000 2.0,5.0 2.0
222249 3 -- c 2021-01-03 00:00:00.000 3.0,6.0 3.0
223250
224- Convert using the pandas-specific method::
251+ Convert using the pandas-specific method:
252+
253+ .. doctest-requires :: pandas
225254
226255 >>> df_pandas = t.to_pandas()
227256 >>> df_pandas
@@ -230,12 +259,16 @@ Convert using the pandas-specific method::
230259 1 <NA> 2.0 b 2021-01-02 2.0 5.0 2.0
231260 2 3 NaN c 2021-01-03 3.0 6.0 3.0
232261
233- Convert using the generic method to pandas::
262+ Convert using the generic method to pandas:
263+
264+ .. doctest-requires :: pandas
234265
235266 >>> df_generic = t.to_df(" pandas" )
236267 >>> # Results are identical to df_pandas
237268
238- Convert to polars using the generic method::
269+ Convert to polars using the generic method:
270+
271+ .. doctest-requires :: polars
239272
240273 >>> df_polars = t.to_df(" polars" )
241274 >>> df_polars
@@ -250,10 +283,15 @@ Convert to polars using the generic method::
250283 │ 3 ┆ null ┆ c ┆ 2021-01-03 00:00:00 ┆ 3.0 ┆ 6.0 ┆ 3.0 │
251284 └──────┴──────┴──────┴─────────────────────┴───────┴────────┴─────┘
252285
253- Convert back to tables::
286+ Convert back to tables:
287+
288+ .. doctest-requires :: pandas
254289
255290 >>> t_from_pandas = QTable.from_pandas(df_pandas) # Using pandas-specific method
256291 >>> t_from_generic = QTable.from_df(df_pandas) # Using generic method
292+
293+ .. doctest-requires :: polars
294+
257295 >>> t_from_polars = QTable.from_df(df_polars) # From polars DataFrame
258296
259297Note the data transformations that occurred:
0 commit comments