admin 管理员组

文章数量: 1086019

Minimum working example:

from datetime import time
import pandas as pd

df = pd.DataFrame(index=['ABC','DEF'], data={time(9):[2,4],time(10):[6,8]})
df.to_parquet('MWE.parquet')
df1 = pd.read_parquet('MWE.parquet')

Error message:

TypeError: data type 'time' not understood

Is there a simple workaround for this?

Minimum working example:

from datetime import time
import pandas as pd

df = pd.DataFrame(index=['ABC','DEF'], data={time(9):[2,4],time(10):[6,8]})
df.to_parquet('MWE.parquet')
df1 = pd.read_parquet('MWE.parquet')

Error message:

TypeError: data type 'time' not understood

Is there a simple workaround for this?

Share Improve this question edited Mar 27 at 16:29 toolic 62.3k20 gold badges79 silver badges128 bronze badges asked Mar 27 at 16:22 mattghgmattghg 1112 bronze badges 3
  • how are you creating the dataframe, just like this? You should consider using native pandas time dtypes, here you are using python a python time type. – juanpa.arrivillaga Commented Mar 27 at 18:42
  • so, in this case, use something like: pd.timedelta_range(start=pd.offsets.Hour(9), end=pd.offsets.Hour(10), freq= '1h') for the columns – juanpa.arrivillaga Commented Mar 27 at 18:44
  • @juanpa.arrivillaga The time data originate in a DateTimeIndex to which I've applied the .time() method. – mattghg Commented Mar 27 at 21:16
Add a comment  | 

1 Answer 1

Reset to default 2

You could convert your column labels to Strings:

from datetime import time
import pandas as pd
df = pd.DataFrame(index=['ABC','DEF'], data={str(time(9)):[2,4],str(time(10)):[6,8]})
df.to_parquet('MWE.parquet')
df1 = pd.read_parquet('MWE.parquet')
print(df1)

gives:

     09:00:00  10:00:00
ABC         2         6
DEF         4         8

本文标签: pythonPandas readparquet can39t understand time data type in dataframe columnsStack Overflow