@@ -2344,3 +2344,169 @@ def _df_set_column_unicode_key_impl(self, key, value):
23442344
23452345 ty_checker = TypeChecker ('Method _set_column().' )
23462346 ty_checker .raise_exc (key , 'str' , 'key' )
2347+
2348+
2349+ def sdc_pandas_dataframe_reset_index_codegen (drop , all_params , columns ):
2350+ """
2351+ Example of generated implementation:
2352+ def _df_reset_index_impl(self, level=None, drop=False, inplace=False, col_level=0, col_fill=""):
2353+ old_index = self.index
2354+ result_0 = get_dataframe_data(self, 0)
2355+ result_1 = get_dataframe_data(self, 1)
2356+ result_2 = get_dataframe_data(self, 2)
2357+ return pandas.DataFrame({"index": old_index, "A": result_0, "B": result_1, "C": result_2})
2358+ """
2359+ result_name = []
2360+ all_params_str = ', ' .join (all_params )
2361+ func_lines = [f'def _df_reset_index_impl({ all_params_str } ):' ]
2362+ df = all_params [0 ]
2363+ if not drop .literal_value :
2364+ old_index = 'old_index'
2365+ func_lines += [f' { old_index } = { df } .index' ]
2366+ result_name .append ((old_index , 'index' ))
2367+ for i , c in enumerate (columns ):
2368+ result_c = f'result_{ i } '
2369+ func_lines += [
2370+ f' result_{ i } = get_dataframe_data({ df } , { i } )'
2371+ ]
2372+ result_name .append ((result_c , c ))
2373+ data = ', ' .join (f'"{ column_name } ": { column } ' for column , column_name in result_name )
2374+ func_lines += [f' return pandas.DataFrame({{{ data } }})' ]
2375+ func_text = '\n ' .join (func_lines )
2376+
2377+ global_vars = {'pandas' : pandas ,
2378+ 'numpy' : numpy ,
2379+ 'get_dataframe_data' : get_dataframe_data }
2380+
2381+ return func_text , global_vars
2382+
2383+
2384+ def sdc_pandas_dataframe_reset_index_impl (self , drop = False ):
2385+ all_params = ['self' , 'level=None' , 'drop=False' , 'inplace=False' , 'col_level=0' , 'col_fill=""' ]
2386+
2387+ func_text , global_vars = sdc_pandas_dataframe_reset_index_codegen (drop , all_params , self .columns )
2388+ loc_vars = {}
2389+ exec (func_text , global_vars , loc_vars )
2390+ _apply_impl = loc_vars [f'_df_reset_index_impl' ]
2391+
2392+ return _apply_impl
2393+
2394+
2395+ def sdc_pandas_dataframe_reset_index_default_codegen (drop , all_params , columns ):
2396+ """
2397+ Example of generated implementation:
2398+ def _df_reset_index_impl(self, level=None, drop=False, inplace=False, col_level=0, col_fill=""):
2399+ old_index = self.index
2400+ result_0 = get_dataframe_data(self, 0)
2401+ result_1 = get_dataframe_data(self, 1)
2402+ return pandas.DataFrame({"index": old_index, "A": result_0, "B": result_1})
2403+ """
2404+ result_name = []
2405+ all_params_str = ', ' .join (all_params )
2406+ func_lines = [f'def _df_reset_index_impl({ all_params_str } ):' ]
2407+ df = all_params [0 ]
2408+ if not drop :
2409+ old_index = 'old_index'
2410+ func_lines += [f' { old_index } = { df } .index' ]
2411+ result_name .append ((old_index , 'index' ))
2412+ for i , c in enumerate (columns ):
2413+ result_c = f'result_{ i } '
2414+ func_lines += [
2415+ f' result_{ i } = get_dataframe_data({ df } , { i } )'
2416+ ]
2417+ result_name .append ((result_c , c ))
2418+ data = ', ' .join (f'"{ column_name } ": { column } ' for column , column_name in result_name )
2419+ func_lines += [f' return pandas.DataFrame({{{ data } }})' ]
2420+ func_text = '\n ' .join (func_lines )
2421+
2422+ global_vars = {'pandas' : pandas ,
2423+ 'numpy' : numpy ,
2424+ 'get_dataframe_data' : get_dataframe_data }
2425+
2426+ return func_text , global_vars
2427+
2428+
2429+ def sdc_pandas_dataframe_reset_index_impl_default (self , drop = False ):
2430+ all_params = ['self' , 'level=None' , 'drop=False' , 'inplace=False' , 'col_level=0' , 'col_fill=""' ]
2431+
2432+ func_text , global_vars = sdc_pandas_dataframe_reset_index_default_codegen (drop , all_params , self .columns )
2433+ loc_vars = {}
2434+ exec (func_text , global_vars , loc_vars )
2435+ _apply_impl = loc_vars [f'_df_reset_index_impl' ]
2436+
2437+ return _apply_impl
2438+
2439+
2440+ @sdc_overload_method (DataFrameType , 'reset_index' )
2441+ def sdc_pandas_dataframe_reset_index (self , level = None , drop = False , inplace = False , col_level = 0 , col_fill = '' ):
2442+ """
2443+ Intel Scalable Dataframe Compiler User Guide
2444+ ********************************************
2445+ Pandas API: pandas.DataFrame.reset_index
2446+
2447+ Limitations
2448+ -----------
2449+ - Reset the index of the DataFrame, and use the default one instead.
2450+ - Parameters level, inplacem col_level, col_fill unsupported.
2451+ - Parameter drop can be only literal value or default value.
2452+
2453+ Examples
2454+ --------
2455+ .. literalinclude:: ../../../examples/dataframe/dataframe_reset_index_drop_False.py
2456+ :language: python
2457+ :lines: 36-
2458+ :caption: Reset the index of the DataFrame, and use the default one instead.
2459+ The old index becomes the first column.
2460+ :name: ex_dataframe_reset_index
2461+
2462+ .. command-output:: python ./dataframe/dataframe_reset_index_drop_False.py
2463+ :cwd: ../../../examples
2464+
2465+ .. literalinclude:: ../../../examples/dataframe/dataframe_reset_index_drop_True.py
2466+ :language: python
2467+ :lines: 36-
2468+ :caption: Reset the index of the DataFrame, and use the default one instead.
2469+ :name: ex_dataframe_reset_index
2470+
2471+ .. command-output:: python ./dataframe/dataframe_reset_index_drop_True.py
2472+ :cwd: ../../../examples
2473+
2474+ Intel Scalable Dataframe Compiler Developer Guide
2475+ *************************************************
2476+ Pandas DataFrame method :meth:`pandas.DataFrame.reset_index` implementation.
2477+
2478+ .. only:: developer
2479+
2480+ Test: python -m sdc.runtests -k sdc.tests.test_dataframe.TestDataFrame.test_df_reset_index*
2481+ """
2482+
2483+ func_name = 'reset_index'
2484+
2485+ ty_checker = TypeChecker ('Method {}().' .format (func_name ))
2486+ ty_checker .check (self , DataFrameType )
2487+
2488+ if not (level is None or isinstance (level , types .Omitted )):
2489+ raise TypingError ('{} Unsupported parameter level. Given: {}' .format (func_name , level ))
2490+
2491+ if not (isinstance (drop , (types .Omitted , types .Boolean )) or drop is False ):
2492+ ty_checker .raise_exc (drop , 'bool' , 'drop' )
2493+
2494+ if isinstance (drop , types .Omitted ):
2495+ drop = False
2496+
2497+ if not (inplace is False or isinstance (inplace , types .Omitted )):
2498+ raise TypingError ('{} Unsupported parameter inplace. Given: {}' .format (func_name , inplace ))
2499+
2500+ if not (col_level == 0 or isinstance (col_level , types .Omitted )):
2501+ raise TypingError ('{} Unsupported parameter col_level. Given: {}' .format (func_name , col_level ))
2502+
2503+ if not (col_fill == '' or isinstance (col_fill , types .Omitted )):
2504+ raise TypingError ('{} Unsupported parameter col_fill. Given: {}' .format (func_name , col_fill ))
2505+
2506+ if not isinstance (drop , types .Literal ):
2507+ if isinstance (drop , bool ):
2508+ return sdc_pandas_dataframe_reset_index_impl_default (self , drop = drop )
2509+ else :
2510+ raise SDCLimitation ('{} only work with Boolean literals drop.' .format (func_name ))
2511+
2512+ return sdc_pandas_dataframe_reset_index_impl (self , drop = drop )
0 commit comments