@@ -1598,9 +1598,7 @@ def index(self):
15981598 Index
15991599 """
16001600 from databricks .koalas .indexes import Index , MultiIndex
1601- if len (self ._internal .index_map ) == 0 :
1602- return None
1603- elif len (self ._internal .index_map ) == 1 :
1601+ if len (self ._internal .index_map ) == 1 :
16041602 return Index (self )
16051603 else :
16061604 return MultiIndex (self )
@@ -1860,9 +1858,6 @@ class max type
18601858 lion mammal 80.5 run
18611859 monkey mammal NaN jump
18621860 """
1863- if len (self ._internal .index_map ) == 0 :
1864- raise NotImplementedError ('Can\' t reset index because there is no index.' )
1865-
18661861 multi_index = len (self ._internal .index_map ) > 1
18671862
18681863 def rename (index ):
@@ -2396,13 +2391,13 @@ def to_koalas(self):
23962391
23972392 >>> spark_df = df.to_spark()
23982393 >>> spark_df
2399- DataFrame[__index_level_0__: bigint, col1: bigint, col2: bigint]
2394+ DataFrame[col1: bigint, col2: bigint]
24002395
24012396 >>> kdf = spark_df.to_koalas()
24022397 >>> kdf
2403- __index_level_0__ col1 col2
2404- 0 0 1 3
2405- 1 1 2 4
2398+ col1 col2
2399+ 0 1 3
2400+ 1 2 4
24062401
24072402 Calling to_koalas on a Koalas DataFrame simply returns itself.
24082403
@@ -2507,8 +2502,8 @@ def to_table(self, name: str, format: Optional[str] = None, mode: str = 'error',
25072502
25082503 >>> df.to_table('%s.my_table' % db, partition_cols='date')
25092504 """
2510- self ._sdf .write .saveAsTable (name = name , format = format , mode = mode ,
2511- partitionBy = partition_cols , options = options )
2505+ self .to_spark () .write .saveAsTable (name = name , format = format , mode = mode ,
2506+ partitionBy = partition_cols , options = options )
25122507
25132508 def to_delta (self , path : str , mode : str = 'error' ,
25142509 partition_cols : Union [str , List [str ], None ] = None , ** options ):
@@ -2618,8 +2613,8 @@ def to_parquet(self, path: str, mode: str = 'error',
26182613 ... mode = 'overwrite',
26192614 ... partition_cols=['date', 'country'])
26202615 """
2621- self ._sdf .write .parquet (path = path , mode = mode , partitionBy = partition_cols ,
2622- compression = compression )
2616+ self .to_spark () .write .parquet (
2617+ path = path , mode = mode , partitionBy = partition_cols , compression = compression )
26232618
26242619 def to_spark_io (self , path : Optional [str ] = None , format : Optional [str ] = None ,
26252620 mode : str = 'error' , partition_cols : Union [str , List [str ], None ] = None ,
@@ -2671,13 +2666,16 @@ def to_spark_io(self, path: Optional[str] = None, format: Optional[str] = None,
26712666
26722667 >>> df.to_spark_io(path='%s/to_spark_io/foo.json' % path, format='json')
26732668 """
2674- self ._sdf .write .save (path = path , format = format , mode = mode , partitionBy = partition_cols ,
2675- options = options )
2669+ self .to_spark () .write .save (
2670+ path = path , format = format , mode = mode , partitionBy = partition_cols , options = options )
26762671
26772672 def to_spark (self ):
26782673 """
26792674 Return the current DataFrame as a Spark DataFrame.
26802675
2676+ .. note:: Index information is lost. So, if the index columns are not present in
2677+ actual columns, they are lost.
2678+
26812679 See Also
26822680 --------
26832681 DataFrame.to_koalas
@@ -3667,14 +3665,21 @@ def pivot_table(self, values=None, index=None, columns=None,
36673665 sdf = sdf .fillna (fill_value )
36683666
36693667 if index is not None :
3670- return DataFrame (sdf ).set_index (index )
3668+ data_columns = [column for column in sdf .columns if column not in index ]
3669+ index_map = [(column , column ) for column in index ]
3670+ internal = _InternalFrame (sdf = sdf , data_columns = data_columns , index_map = index_map )
3671+ return DataFrame (internal )
36713672 else :
36723673 if isinstance (values , list ):
36733674 index_values = values [- 1 ]
36743675 else :
36753676 index_values = values
36763677
3677- return DataFrame (sdf .withColumn (columns , F .lit (index_values ))).set_index (columns )
3678+ sdf = sdf .withColumn (columns , F .lit (index_values ))
3679+ data_columns = [column for column in sdf .columns if column not in columns ]
3680+ index_map = [(column , column ) for column in columns ]
3681+ internal = _InternalFrame (sdf = sdf , data_columns = data_columns , index_map = index_map )
3682+ return DataFrame (internal )
36783683
36793684 def pivot (self , index = None , columns = None , values = None ):
36803685 """
@@ -4378,9 +4383,6 @@ def sort_index(self, axis: int = 0,
43784383 a 1 2 1
43794384 b 1 0 3
43804385 """
4381- if len (self ._internal .index_map ) == 0 :
4382- raise ValueError ("Index should be set." )
4383-
43844386 if axis != 0 :
43854387 raise ValueError ("No other axes than 0 are supported at the moment" )
43864388 if kind is not None :
0 commit comments