0

I know this should be really easy but I keep getting errors when trying to pivot. I'm getting my data from psycopg2

cur.execute("select * from table")
arr=cur.fetchall()
pdres=pandas.DataFrame(arr, columns=['pricedate',hour', 'node','dart'])
pdres=pdres.set_index(['pricedate','hour','node'])

Here is where stuff stops working...

pdres.pivot(index=['pricedate','hour'],columns='node',values='dart')

results in Wrong number of items passed 720, placement implies 2

That brought me to this question which led me to doing this

pandas.pivot_table(pdres, index=['pricedate','hour'], columns='node', values='dart')

This tells me 'No numeric types to aggregate

That led me to http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.convert_objects.html but I'm not sure the right syntax of it (when to use it), and I also don't even know if I'm on the right track.

Community
  • 1
  • 1
Dean MacGregor
  • 11,847
  • 9
  • 34
  • 72

1 Answers1

0

There are two changes to make it work:

  • First don't set_index
  • Second, don't use convert_objects. Do use astype

Specifically, the following works:

cur.execute("select * from table")
arr = cur.fetchall()
pdres = pandas.DataFrame(arr, columns=['pricedate', 'hour', 'node', 'dart'])
pdres['dart'] = pdres['dart'].astype(float)
pdres = pandas.pivot_table(pdres, index=['pricedate','hour'], columns='node', values='dart')
metasequoia
  • 7,014
  • 5
  • 41
  • 54
Dean MacGregor
  • 11,847
  • 9
  • 34
  • 72