33"""
44import narwhals .stable .v1 as nw
55
6- def gapminder (datetimes = False , centroids = False , year = None , pretty_names = False , return_type = "pandas" ):
6+
7+ def gapminder (
8+ datetimes = False ,
9+ centroids = False ,
10+ year = None ,
11+ pretty_names = False ,
12+ return_type = "pandas" ,
13+ ):
714 """
815 Each row represents a country on a given year.
916
@@ -17,11 +24,17 @@ def gapminder(datetimes=False, centroids=False, year=None, pretty_names=False, r
1724 If `centroids` is True, two new columns are added: ['centroid_lat', 'centroid_lon']
1825 If `year` is an integer, the dataset will be filtered for that year
1926 """
20- df = nw .from_native (_get_dataset ("gapminder" , return_type = return_type ), eager_only = True )
27+ df = nw .from_native (
28+ _get_dataset ("gapminder" , return_type = return_type ), eager_only = True
29+ )
2130 if year :
2231 df = df .filter (nw .col ("year" ) == year )
2332 if datetimes :
24- df = df .with_columns (nw .concat_str ([nw .col ("year" ).cast (nw .String ()), nw .lit ("-01-01" )]).cast (nw .Datetime (time_unit = "ns" )))
33+ df = df .with_columns (
34+ nw .concat_str ([nw .col ("year" ).cast (nw .String ()), nw .lit ("-01-01" )]).cast (
35+ nw .Datetime (time_unit = "ns" )
36+ )
37+ )
2538 if not centroids :
2639 df = df .drop ("centroid_lat" , "centroid_lon" )
2740 if pretty_names :
@@ -149,10 +162,12 @@ def stocks(indexed=False, datetimes=False, return_type="pandas"):
149162 msg = "Cannot set index for backend different from pandas"
150163 raise NotImplementedError (msg )
151164
152- df = nw .from_native (_get_dataset ("stocks" , return_type = return_type ), eager_only = True )
165+ df = nw .from_native (
166+ _get_dataset ("stocks" , return_type = return_type ), eager_only = True
167+ )
153168 if datetimes :
154169 df = df .with_columns (nw .col ("date" ).cast (nw .Datetime (time_unit = "ns" )))
155-
170+
156171 if indexed : # then it must be pandas
157172 df = df .to_native ().set_index ("date" )
158173 df .columns .name = "company"
@@ -171,12 +186,14 @@ def experiment(indexed=False, return_type="pandas"):
171186 A `pandas.DataFrame` with 100 rows and the following columns:
172187 `['experiment_1', 'experiment_2', 'experiment_3', 'gender', 'group']`.
173188 If `indexed` is True, the data frame index is named "participant" """
174-
189+
175190 if indexed and return_type != "pandas" :
176191 msg = "Cannot set index for backend different from pandas"
177192 raise NotImplementedError (msg )
178193
179- df = nw .from_native (_get_dataset ("experiment" , return_type = return_type ), eager_only = True )
194+ df = nw .from_native (
195+ _get_dataset ("experiment" , return_type = return_type ), eager_only = True
196+ )
180197 if indexed : # then it must be pandas
181198 df = df .to_native ()
182199 df .index .name = "participant"
@@ -194,12 +211,14 @@ def medals_wide(indexed=False, return_type="pandas"):
194211 `['nation', 'gold', 'silver', 'bronze']`.
195212 If `indexed` is True, the 'nation' column is used as the index and the column index
196213 is named 'medal'"""
197-
214+
198215 if indexed and return_type != "pandas" :
199216 msg = "Cannot set index for backend different from pandas"
200217 raise NotImplementedError (msg )
201218
202- df = nw .from_native (_get_dataset ("medals" , return_type = return_type ), eager_only = True )
219+ df = nw .from_native (
220+ _get_dataset ("medals" , return_type = return_type ), eager_only = True
221+ )
203222 if indexed : # then it must be pandas
204223 df = df .to_native ().set_index ("nation" )
205224 df .columns .name = "medal"
@@ -216,18 +235,18 @@ def medals_long(indexed=False, return_type="pandas"):
216235 A `pandas.DataFrame` with 9 rows and the following columns:
217236 `['nation', 'medal', 'count']`.
218237 If `indexed` is True, the 'nation' column is used as the index."""
219-
238+
220239 if indexed and return_type != "pandas" :
221240 msg = "Cannot set index for backend different from pandas"
222241 raise NotImplementedError (msg )
223-
224- df = (
225- nw . from_native ( _get_dataset ("medals" , return_type = return_type ), eager_only = True )
226- .unpivot (
227- index = ["nation" ],
228- value_name = "count" ,
229- variable_name = "medal" ,
230- ) )
242+
243+ df = nw . from_native (
244+ _get_dataset ("medals" , return_type = return_type ), eager_only = True
245+ ) .unpivot (
246+ index = ["nation" ],
247+ value_name = "count" ,
248+ variable_name = "medal" ,
249+ )
231250 if indexed :
232251 df = nw .maybe_set_index (df , "nation" )
233252 return df .to_native ()
0 commit comments