Skip to content

Commit ef196cb

Browse files
ueshinHyukjinKwon
authored andcommitted
Add support from_pandas for Index/MultiIndex. (#1170)
1 parent f9a4180 commit ef196cb

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

databricks/koalas/namespace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def from_pandas(pobj: Union['pd.DataFrame', 'pd.Series']) -> Union['Series', 'Da
6868
return Series(pobj)
6969
elif isinstance(pobj, pd.DataFrame):
7070
return DataFrame(pobj)
71+
elif isinstance(pobj, pd.Index):
72+
return DataFrame(pd.DataFrame(index=pobj)).index
7173
else:
7274
raise ValueError("Unknown data type: {}".format(type(pobj)))
7375

databricks/koalas/tests/test_namespace.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,27 @@ def test_from_pandas(self):
2727
'month': [2, 3],
2828
'day': [4, 5]})
2929
kdf = ks.from_pandas(pdf)
30+
31+
self.assert_eq(kdf, pdf)
32+
33+
pser = pdf.year
34+
kser = ks.from_pandas(pser)
35+
36+
self.assert_eq(kser, pser)
37+
3038
pidx = pdf.index
31-
kidx = kdf.index
39+
kidx = ks.from_pandas(pidx)
40+
41+
self.assert_eq(kidx, pidx)
42+
43+
pmidx = pdf.set_index('year', append=True).index
44+
kmidx = ks.from_pandas(pmidx)
45+
46+
self.assert_eq(kmidx, pmidx)
3247

3348
expected_error_message = 'Unknown data type: {}'.format(type(kidx))
3449
with self.assertRaisesRegex(ValueError, expected_error_message):
3550
ks.from_pandas(kidx)
36-
expected_error_message = 'Unknown data type: {}'.format(type(pidx))
37-
with self.assertRaisesRegex(ValueError, expected_error_message):
38-
ks.from_pandas(pidx)
3951

4052
def test_to_datetime(self):
4153
pdf = pd.DataFrame({'year': [2015, 2016],

0 commit comments

Comments
 (0)