superset icon indicating copy to clipboard operation
superset copied to clipboard

Oracle backend: pandas Exception : KeyError

Open Kikiero opened this issue 10 years ago • 30 comments

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/views.py", line 827, in explore
    payload = obj.get_json()
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 294, in get_json
    'data': self.get_data(),
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 479, in get_data
    return self.get_df().to_html(
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 474, in get_df
    margins=True,
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/tools/pivot.py", line 120, in pivot_table
    grouped = data.groupby(keys)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/generic.py", line 3778, in groupby
    **kwargs)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 1427, in groupby
    return klass(obj, by, **kwds)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 354, in __init__
    mutated=self.mutated)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 2383, in _get_grouper
    in_axis, name, gpr = True, gpr, obj[gpr]
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'agent_name'

Kikiero avatar Aug 16 '16 07:08 Kikiero

My caravel version is : 0.10.0 Download url is : https://github.com/airbnb/caravel/tarball/0.10.0

piovt table: When I was chosen 『 Group by 』、 『Columns』、 『Metrics』、『Aggregation function』 then Tap enter button

Kikiero avatar Aug 16 '16 07:08 Kikiero

subjet: # column_name ---> upper(column_name) cause exceptions

1.# test.py

#!/usr/bin/python import pandas as pd import numpy as np

d={} d['a']=['1','2'] d['b']=['3','4'] d['c']=['2','2']

df = pd.DataFrame(d)

print('--------- 1 ----------') print(df.pivot_table( values='c', index=['a'],columns=['b'], aggfunc=np.sum,margins=True))

print('--------- 2 ----------') print(pd.pivot_table(df, values='c', index=['a'],columns=['b'], aggfunc=np.sum,margins=True))

print('--------- 3 ----------') print(df.pivot_table( values='c', index=['a'],columns=['B'], aggfunc=np.sum,margins=True))

print('--------- 4 ----------') print(pd.pivot_table(df, values='c', index=['a'],columns=['B'], aggfunc=np.sum,margins=True))

  1. # change column_name 'b' to 'B' then run test.py

--------- 1 ---------- b 3 4 All a
1 2 None 2 2 None 2 2 All 2 2 22 --------- 2 ---------- b 3 4 All a
1 2 None 2 2 None 2 2 All 2 2 22 --------- 3 ---------- Traceback (most recent call last): File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/indexes/base.py", line 1945, in get_loc return self._engine.get_loc(key) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'a'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/root/test/a.py", line 19, in print(df.pivot_table( values='c', index=['a'],columns=['B'], aggfunc=np.sum,margins=True)) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/tools/pivot.py", line 158, in pivot_table margins_name=margins_name) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/tools/pivot.py", line 205, in _add_margins margins_name) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/tools/pivot.py", line 283, in _generate_marginal_results piece[all_key] = margin[key] File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in getitem return self._getitem_column(key) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column return self._get_item_cache(key) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache values = self._data.get(item) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get loc = self.items.get_loc(item) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'a'

Kikiero avatar Aug 18 '16 08:08 Kikiero

@PickLiDan that may or may be not be the cause :) To find out, just print the keys available in the dataframe so we can't see what's wrong here.

xrmx avatar Aug 18 '16 10:08 xrmx

@xrmx # i change my column to upper case,then i work well,so i am sure.

lower case :

2016-08-18 20:08:14,614:INFO:root: DT COUNT_DISTINCT__ORDER_ID 0 2016-08-13 930613 1 2016-08-14 877308 2 2016-08-12 791172 3 2016-08-15 630725 4 2016-08-16 4595 2016-08-18 20:08:14,616:INFO:root:Serving from groupby:dt 2016-08-18 20:08:14,616:INFO:root:Serving from columns: 2016-08-18 20:08:14,616:INFO:root:Serving from metrics:COUNT_DISTINCT__ORDER_ID 2016-08-18 20:08:14,616:INFO:root:Serving from pandas_aggfunc : s,u,m 2016-08-18 20:08:14,617:ERROR:root:'dt' Traceback (most recent call last): File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/indexes/base.py", line 1945, in get_loc return self._engine.get_loc(key) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'dt'

During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/views.py", line 923, in explore payload = obj.get_json() File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 299, in get_json 'data': self.get_data(), File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 500, in get_data return self.get_df().to_html( File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/caravel/viz.py", line 495, in get_df margins=True File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/tools/pivot.py", line 120, in pivot_table grouped = data.groupby(keys) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/generic.py", line 3778, in groupby *_kwargs) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 1427, in groupby return klass(obj, by, *_kwds) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 354, in init mutated=self.mutated) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/groupby.py", line 2383, in _get_grouper in_axis, name, gpr = True, gpr, obj[gpr] File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in getitem return self._getitem_column(key) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column return self._get_item_cache(key) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache values = self._data.get(item) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get loc = self.items.get_loc(item) File "/usr/local/Python-3.5.2/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'dt'

upper case :

2016-08-18 20:03:51,109:INFO:root: DT COUNT_DISTINCT__ORDER_ID 0 2016-08-13 930613 1 2016-08-14 877308 2 2016-08-12 791172 3 2016-08-11 712151 4 2016-08-15 630725 5 2016-08-16 4595 2016-08-18 20:03:51,111:INFO:root:Serving from groupby:DT 2016-08-18 20:03:51,111:INFO:root:Serving from columns: 2016-08-18 20:03:51,111:INFO:root:Serving from metrics:COUNT_DISTINCT__ORDER_ID 2016-08-18 20:03:51,111:INFO:root:Serving from pandas_aggfunc : s,u,m 2016-08-18 20:03:51,132:INFO:root:Caching for the next None seconds

Kikiero avatar Aug 18 '16 12:08 Kikiero

@PickLiDan so to sum up, you have a field in your table called "DT" all uppercase, but on the select widget of group by you have something that has value "dt" lowercase?

xrmx avatar Aug 18 '16 12:08 xrmx

yeah . I suggest there should be a set, you can set case-insensitive。 My English is poor ,sorry.

Kikiero avatar Aug 19 '16 05:08 Kikiero

I don't see where we could be doing that in the code. Are you sure you just don't have a column in your table model with the wrong name?

xrmx avatar Aug 23 '16 09:08 xrmx

@PickLiDan - could you please provide the screenshots ?

bkyryliuk avatar Sep 22 '16 17:09 bkyryliuk

@PickLiDan - feel free to reopen is the issue persists. I cannot reproduce it.

bkyryliuk avatar Oct 03 '16 17:10 bkyryliuk

I just met exactly the same bug. My case is I'm connectting caravel AKA superset to Apache Kylin and use pykylin as sqlachemy dialect. Dimensions generated by Kylin are all uppercase and columns generated by caravel are lowercase. Thus when I use pivot table or any other charts the bug appears. Screenshot

And it's weird when I uppercase all columns and save, the lowercase columns appear again. It seems pykylin sync from kylin

auroralinan avatar Dec 28 '16 01:12 auroralinan

Hi,

I think I have a similar error.

SELECT date_format( created_at,'%%d-%%m-%%Y')as DT,count(*) as CT FROM user group by DT.

This the query I ran in SQL lab. Got the data accordingly. But when I try to visualize it in Time series line chart, screen shot 2017-02-13 at 3 57 50 pm

I get the following error in logs.

2017-02-13 15:55:10,887:ERROR:root:'CT' Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py", line 1945, in get_loc return self._engine.get_loc(key) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'CT'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/superset/views.py", line 1406, in explore_json payload = viz_obj.get_payload() File "/usr/local/lib/python3.6/site-packages/superset/viz.py", line 360, in get_payload data = self.get_data() File "/usr/local/lib/python3.6/site-packages/superset/viz.py", line 1590, in get_data def get_data(self): File "/usr/local/lib/python3.6/site-packages/superset/viz.py", line 1575, in get_df File "/usr/local/lib/python3.6/site-packages/pandas/core/generic.py", line 3778, in groupby **kwargs) File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby.py", line 1427, in groupby return klass(obj, by, **kwds) File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby.py", line 354, in init mutated=self.mutated) File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby.py", line 2383, in _get_grouper in_axis, name, gpr = True, gpr, obj[gpr] File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 1997, in getitem return self._getitem_column(key) File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2004, in _getitem_column return self._get_item_cache(key) File "/usr/local/lib/python3.6/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache values = self._data.get(item) File "/usr/local/lib/python3.6/site-packages/pandas/core/internals.py", line 3290, in get loc = self.items.get_loc(item) File "/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py", line 1947, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) KeyError: 'CT'

This is what is being displayed on screen. screen shot 2017-02-13 at 3 58 56 pm

pardhasm avatar Feb 13 '17 10:02 pardhasm

Column names become lower case regardless of forcing it to upper case in query. Oracle SQL database is used. If I make column names lowercase, then after selecting things in 'group by', the additional sql surround the original sql will not recognize that lowercase column name. There is no such problem using MySQL database.

yileic avatar Apr 26 '17 22:04 yileic

Has anyone found a work-around for this yet? This is a blocker for me applying it at work because we have to deal with an Oracle DB.

TryHarder01 avatar Aug 28 '17 13:08 TryHarder01

@TryHarder01 my current workaround is editing the column name in the UI after the table is created.

yileic avatar Aug 28 '17 19:08 yileic

@TryHarder01 @yileic I tried casting the column names to upper case and lower case in the UI but it still has the 'KeyError: [column_name]' issue

The error doesn't come up when I choose 'Table View' but when I change the visualization type to any other view, it doesn't recognize the key

nikitathampi avatar Aug 28 '17 19:08 nikitathampi

Are all the ongoing issues here happening on an Oracle db?

mistercrunch avatar Aug 29 '17 03:08 mistercrunch

@mistercrunch Yes, I haven't had any issues on MySQL. Its just been Oracle so far.

nikitathampi avatar Aug 29 '17 13:08 nikitathampi

Would it be helpful to close and reopen as a bug specifically with Oracle DB?

TryHarder01 avatar Aug 29 '17 15:08 TryHarder01

I renamed this issue to make it specific to Oracle. So it appeared to me like the SQLAlchemy dialect for Oracle returns lower case but then Oracle wants upper case at query time?

Can someone with an Oracle database laying around confirm that the issue is with the SQLALchemy driver returning lower case column names?

mistercrunch avatar Aug 30 '17 02:08 mistercrunch

This seems very relevant: https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/dialects/oracle/base.py#L56

Damn Oracle! and don't get me started on the the 30 character limit on object names. Fully committed to bad decisions made in the 80s.

mistercrunch avatar Aug 30 '17 02:08 mistercrunch