【问题标题】:dataframe parsed as a tuple, when read from a pickle从泡菜中读取时,数据帧被解析为元组
【发布时间】:2019-10-16 16:53:58
【问题描述】:

我有一个包含数据帧字典的 pickle 文件。作为数据清理脚本的一部分,我加载这个 pickle 并对一些(但不是全部)数据帧进行额外处理,然后覆盖 pickle 以供模拟程序稍后拾取和加载。

当我在这个处理之后读取泡菜时,除了两个值之外的所有值都被正确解包并解析为数据帧,但是这两个值被读取为元组。由于这两个实际上不需要在此特定数据清理脚本中进行任何更改,因此脚本不会对它们进行处理,超出以下范围:

#start of script, read in the pickle assign the dfs for later use.
input_file = sys.argv[1]
with open(input_file, 'rb') as handle:
  data = pickle.load(handle)


trips      = data['trips']       # this sees additional processing, is correctly written out as a DF. 
stops      = data['stops']       # this sees additional processing, is correctly written out as a DF.
stop_times = data['stop_times'], # NO additional processing, is INCORRECTLY written out as a tuple.
road_segs  = data['road_segs'],  # NO additional processing, is INCORRECTLY written out as a tuple.
seg_props  = data['seg_props']   # NO additional processing, is correctly written out as a df.


... # do additional processing on trips and stops


#Output the update DFs and carry the unaltered DFs through to overwrite the original pickle.

data = {
  "trips":      trips,
  "stops":      stops,
  "stop_times": stop_times,
  "road_segs":  road_segs,
  "seg_props":  seg_props
}

with open(input_file, 'wb') as handle:
  pickle.dump(data, handle, protocol=4)

如果我在运行这个脚本之前阅读了泡菜,我会得到以下信息。

[type(val) for val in gtfs.values()]                                                                                                                                                    
#output
[pandas.core.frame.DataFrame,
 geopandas.geodataframe.GeoDataFrame,
 pandas.core.frame.DataFrame,
 pandas.core.frame.DataFrame,
 pandas.core.frame.DataFrame]

之后:

[type(val) for val in gtfs.values()]                                                                                                                                                    
Out[17]: 
[pandas.core.frame.DataFrame,
 pandas.core.frame.DataFrame,
 tuple,
 tuple,
 pandas.core.frame.DataFrame]

这些元组也是高度嵌套的:

(((                                   trip_id stop_id  stop_duation
   0        15243854-AUG19-MVS-BUS-Weekday-01   17894           0.0
   1        15243854-AUG19-MVS-BUS-Weekday-01   17897           0.0
   2        15243854-AUG19-MVS-BUS-Weekday-01   17900           0.0

   [2812369 rows x 3 columns],),),)

【问题讨论】:

    标签: python pandas pickle


    【解决方案1】:

    我有两个逗号

    stop_times = data['stop_times'],
    road_segs  = data['road_segs'],
    

    在我的导入中,这是造成这种情况的原因。一遍又一遍地盯着它,我怎么没有注意到这一点,这超出了我的想象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多