koalas icon indicating copy to clipboard operation
koalas copied to clipboard

Series(data=df, index=None) seems to be incorrectly instantiated

Open LucasG0 opened this issue 6 years ago • 10 comments

Hi, I saw it is now possible to create a Series from a DataFrame without any additional index parameter. However, I can not print or sort_index a Series which has been created in this way. I guess the Series is incorrectly instantiated and probably other actions would fail too.

>>> df = ks.DataFrame({"a": [1, 2, 3, 4, 5]})
>>> ser = ks.Series(df)
>>> print(ser)
Traceback (most recent call last):
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Dev\koalas\databricks\koalas\series.py", line 5232, in __repr__
    pser = self._kdf._get_or_create_repr_pandas_cache(max_display_count)[self.name]
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: None

>>> ser.sort_index()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Dev\koalas\databricks\koalas\series.py", line 2227, in sort_index
    kdf = self._kdf[[self.name]].sort_index(
  File "D:\Dev\koalas\databricks\koalas\frame.py", line 10130, in __getitem__
    return self.loc[:, list(key)]
  File "D:\Dev\koalas\databricks\koalas\indexing.py", line 441, in __getitem__
    cols_sel
  File "D:\Dev\koalas\databricks\koalas\indexing.py", line 313, in _select_cols
    return self._select_cols_by_iterable(cols_sel, missing_keys)
  File "D:\Dev\koalas\databricks\koalas\indexing.py", line 1175, in _select_cols_by_iterable
    raise KeyError("['{}'] not in index".format(name_like_string(key)))
KeyError: "['__none__'] not in index"

LucasG0 avatar Aug 27 '20 15:08 LucasG0

Can you share the full error message? Looks like you're running it in Windows, is it correct?

HyukjinKwon avatar Aug 28 '20 03:08 HyukjinKwon

Yes I am running it on Windows, I edited with full error message.

LucasG0 avatar Aug 28 '20 03:08 LucasG0

@LucasG0, are you able to create a pandas DataFrame and select via df[column_name]?

HyukjinKwon avatar Aug 28 '20 04:08 HyukjinKwon

Yes, using pandas 1.0.5.

>>> df = pd.DataFrame({"a": [1, 1, 1], "b": [2, 2, 2]})
>>> df['a']
0    1
1    1
2    1
Name: a, dtype: int64
>>> df['b']
0    2
1    2
2    2
Name: b, dtype: int64

LucasG0 avatar Aug 28 '20 11:08 LucasG0

Actually, it fails with an index parameter too.

>>> df = ks.DataFrame({"a": [1, 2, 3, 4, 5]})
>>> ser = ks.Series(df, index=df.index.to_numpy())
>>> print(ser)
Traceback (most recent call last):
  File "mytest.py", line 22, in <module>
    print(ser)
  File "D:\Dev\koalas\databricks\koalas\series.py", line 5239, in __repr__
    pser = self._kdf._get_or_create_repr_pandas_cache(max_display_count)[self.name]
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\frame.py", line 2806, in __getitem__
    indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\indexing.py", line 1553, in _get_listlike_indexer
    keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
  File "D:\Dev\Utils\Miniconda\envs\koalas-dev\lib\site-packages\pandas\core\indexing.py", line 1640, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Int64Index([0, 1, 2, 3, 4], dtype='int64')] are in the [columns]"

LucasG0 avatar Aug 28 '20 17:08 LucasG0

I guess, passing DataFrame to the constructor of Series is not supported?

ueshin avatar Aug 28 '20 17:08 ueshin

In deed Series doc does not specify that DataFrame can be passed as data. But the constructor seems to handle it so I wonder if this behavior is intended.

LucasG0 avatar Aug 29 '20 10:08 LucasG0

Ah, yeah, but actually the constructor taking DataFrame is intended for internal use so far. What's the behavior of pandas?

ueshin avatar Aug 29 '20 17:08 ueshin

Alright, pandas does not support it either. For internal purposes (#1737), I actually found the function first_series instead of passing DataFrame as Series constructor parameter.

LucasG0 avatar Aug 30 '20 15:08 LucasG0

I'll tentatively resolve this ticket since pandas doesn't support either.

HyukjinKwon avatar Aug 31 '20 00:08 HyukjinKwon