0

I'm trying to initialize a data frame with Int64 data type but keep getting a TypeError:

>>> df = pd.DataFrame(index=[1,2,3], columns=['a','b'], dtype='Int64')

TypeError: values must be a 1D list-like

What can I do?

HappyPy
  • 9,839
  • 13
  • 46
  • 68
  • 1
    It seems like a bug in pandas. Maybe construct with floats and then cast to Int64? `df = pd.DataFrame(index=[1,2,3], columns=['a','b']).astype('Int64') ` – ayhan Jan 06 '20 at 22:13
  • Thanks @ayhan, that works, although `df['a'] = pd.DataFrame(index=[1,2],data={'a':[1,3]})` seems to revert `a` to `float64` again. But I guess I can use the same trick on the new data frame – HappyPy Jan 06 '20 at 22:20
  • Why are you initializing an empty DataFrame? I'm a little worried that you're going down the route of [this question](https://stackoverflow.com/a/56746204/4909087). – cs95 Jan 06 '20 at 22:41
  • I agree with @cs95, some more context/clarification would be nice. – AMC Jan 06 '20 at 23:33

1 Answers1

0

I think you should this

df = pd.DataFrame(data=[pd.Series()]*3,index=[1,2,3], columns=['a','b'], dtype='Int64')
meph
  • 662
  • 3
  • 10