Python 将数据帧从pandas转换为pyspark转换为foundry时的数据类型

Python 将数据帧从pandas转换为pyspark转换为foundry时的数据类型,python,pandas,pyspark,cloud-foundry,Python,Pandas,Pyspark,Cloud Foundry,对于那些在Foundry环境中工作的人,我尝试在“代码存储库”中构建一个管道,将原始数据集(来自Excel文件)处理成一个干净的数据集,稍后我将在“Contour”中进行分析。 为此,我使用了python,除了管道似乎使用了pyspark,在某个时候,我必须将我用pandas清理的数据集转换为pyspark数据集,这就是我遇到的问题 我已经看过几个关于stackover flow的帖子,将Pandas DF转换为Pyspark DF,但到目前为止,似乎没有一个解决方案有效。 当我尝试运行转换时,

对于那些在Foundry环境中工作的人,我尝试在“代码存储库”中构建一个管道,将原始数据集(来自Excel文件)处理成一个干净的数据集,稍后我将在“Contour”中进行分析。 为此,我使用了python,除了管道似乎使用了pyspark,在某个时候,我必须将我用pandas清理的数据集转换为pyspark数据集,这就是我遇到的问题

我已经看过几个关于stackover flow的帖子,将Pandas DF转换为Pyspark DF,但到目前为止,似乎没有一个解决方案有效。 当我尝试运行转换时,尽管我强制使用了一个模式,但总有一个数据类型无法转换

Python代码部分已在Spyder中成功测试(导入和导出有一个Excel文件),并给出了预期结果。只有当我需要转换为pyspark时,它才会以某种方式失败

@transform_pandas(
    Output("/MDM_OUT_OF_SERVICE_EVENTS_CLEAN"),
    OOS_raw=Input("/MDM_OUT_OF_SERVICE_EVENTS"),
)
def DA_transform(OOS_raw):

''' Code Section in Python '''

  mySchema=StructType([StructField(OOS_dup.columns[0], IntegerType(), 
                   True),
                   StructField(OOS_dup.columns[1], StringType(), True),
                   ...])

  OOS_out=sqlContext.createDataFrame(OOS_dup,schema 
    =mySchema,verifySchema=False)

return OOS_out
我在某个时候收到了此错误消息:

AttributeError: 'unicode' object has no attribute 'toordinal'.
根据这篇文章:

这是因为pyspark无法将数据转换为Datetype

但是数据是在熊猫的
Datetime64[ns]
中。我尝试将这些列转换为字符串和整数,但也失败了

下面是Python输出数据集的图片:

以下是熊猫清理数据集后返回的数据类型:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4972 entries, 0 to 4971
Data columns (total 51 columns):
OOS_ID                       4972 non-null int64
OPERATOR_CODE                4972 non-null object
ATA_CAUSE                    4972 non-null int64
EVENT_CODE                   3122 non-null object
AC_MODEL                     4972 non-null object
AC_SN                        4972 non-null int64
OOS_DATE                     4972 non-null datetime64[ns]
AIRPORT_CODE                 4915 non-null object
RTS_DATE                     4972 non-null datetime64[ns]
EVENT_TYPE                   4972 non-null object
CORRECTIVE_ACTION            417 non-null object
DD_HOURS_OOS                 4972 non-null float64
EVENT_DESCRIPTION            4972 non-null object
EVENT_CATEGORY               4972 non-null object
ATA_REPORTED                 324 non-null float64
TOTAL_CAUSES                 4875 non-null float64
EVENT_NUMBER                 3117 non-null float64
RTS_TIME                     4972 non-null object
OOS_TIME                     4972 non-null object
PREV_REPORTED                4972 non-null object
FERRY_IND                    4972 non-null object
REPAIR_STN_CODE              355 non-null object
MAINT_DOWN_TIME              4972 non-null float64
LOGBOOK_RECORD_IDENTIFIER    343 non-null object
RTS_IND                      4972 non-null object
READY_FOR_USE                924 non-null object
DQ_COMMENTS                  2 non-null object
REVIEWED                     5 non-null object
DOES_NOT_MEET_SPECS          4 non-null object
CORRECTED                    12 non-null object
EDITED_BY                    4972 non-null object
EDIT_DATE                    4972 non-null datetime64[ns]
OUTSTATION_INDICATOR         3801 non-null object
COMMENT_TEXT                 11 non-null object
ATA_CAUSE_CHAPTER            4972 non-null int64
ATA_CAUSE_SECTION            4972 non-null int64
ATA_CAUSE_COMPONENT          770 non-null float64
PROCESSOR_COMMENTS           83 non-null object
PARTS_AVAIL_AT_STATION       4972 non-null object
PARTS_SHIPPED_AT_STATION     4972 non-null object
ENGINEER_AT_STATION          4972 non-null object
ENGINEER_SENT_AT_STATION     4972 non-null object
SOURCE_FILE                  4972 non-null object
OOS_Month                    4972 non-null float64
OOS_Hour                     4972 non-null float64
OOS_Min                      4972 non-null float64
RTS_Month                    4972 non-null float64
RTS_Hour                     4972 non-null float64
RTS_Min                      4972 non-null float64
OOS_Timestamp                4972 non-null datetime64[ns]
RTS_Timestamp                4972 non-null datetime64[ns]
dtypes: datetime64[ns](5), float64(12), int64(5), object(29)

范围索引:4972个条目,0到4971
数据列(共51列):
OOS_ID 4972非空int64
运算符_代码4972非空对象
ATA_原因4972非空int64
事件代码3122非空对象
AC_4972型非空对象
AC_SN 4972非空int64
OOS_日期4972非空日期时间64[ns]
机场代码4915非空对象
RTS_日期4972非空日期时间64[ns]
事件类型4972非空对象
纠正措施417非空对象
DD_小时数_OOS 4972非空浮点64
事件描述4972非空对象
事件\类别4972非空对象
ATA_报告324个非空浮点64
总计导致4875个非空浮点64
事件编号3117非空浮点64
RTS_时间4972非空对象
OOS_时间4972非空对象
上一个报告的4972个非空对象
FERRY_IND 4972非空对象
修复代码355非空对象
维护停机时间4972非空浮点64
日志记录标识符343非空对象
RTS_IND 4972非空对象
准备使用924非空对象
DQ_注释2非空对象
查看了5个非空对象
不符合规格4非空对象吗
修正了12个非空对象
由4972非空对象编辑
编辑日期4972非空datetime64[ns]
分站_指示器3801非空对象
注释\u文本11非空对象
ATA_原因_第4972章非空int64
ATA_原因_第4972节非空int64
ATA_原因_组件770非空浮点64
处理器\u注释83非空对象
零件在工作站4972处可用非空对象
零件装运地点4972非空对象
工程师站4972非空对象
工程师在工作站4972发送非空对象
源文件4972非空对象
OOS_第4972个月非空浮动64
OOS_小时4972非空浮点64
OOS_Min 4972非空浮点64
RTS_第4972个月非空浮动64
RTS_小时4972非空浮点64
RTS_Min 4972非空浮点64
OOS_时间戳4972非空datetime64[ns]
RTS_时间戳4972非空datetime64[ns]
数据类型:datetime64[ns](5)、float64(12)、int64(5)、object(29)

我在官方铸造厂文档中找到了关于如何在pandas和pyspark DF之间正确转换的信息,以防对你们中的一些人有所帮助

OOS_dup是我想要转换回Spark的熊猫数据帧

# Extract the name of each columns with its data type in pandas
    col = OOS_dup.columns
    col_type = list()

    for c in col:
        t = OOS_dup[c].dtype.name
        col_type.append(t)

    df_schema = pd.DataFrame({"field": col, "data_type": col_type})

    # Define a function to replace missing (NaN sky coverage cells with Null
    def replace_missing(df, col_names):
        for col in col_names:
            df = df.withColumn("{}".format(col),
                               F.when(df["{}".format(col)] == "NaN", None).otherwise(df["{}".format(col)]))
        return df

    # Replace missing values
    OOS_dup = replace_missing(OOS_dup, col)

    # Define a function to change column types to the proper type in spark
    def change_type(df, col_names, dtypes):
        for col in col_names:
            df = df.withColumn("{}".format(col), F.when(dtypes == "float64", (df["{}".format(col)]).cast("double")).when(dtypes == "int64", (df["{}".format(col)]).cast("int")).when(dtypes == "datetime64[ns]", (df["{}".format(col)]).cast("date")).otherwise((df["{}".format(col)]).cast("string")))
        return df

    # Cast each columns to the proper data type
    OOS_dup = change_type(OOS_dup, df_schema["field"], df_schema["data_type"])

    OOS_dup = sqlContext.createDataFrame(OOS_dup)

你能展示熊猫数据框吗?嗨,我在主帖子中添加了数据集的图片。谢谢,谢谢。您是否也可以显示熊猫数据帧的现有模式?