Python Azure SDK - 使用 from_delimited_files() 从 blobstore 读取表格数据时推断出不正确的日期时间格式

标签 python azure azure-sdk-python azure-machine-learning-service

我正在使用 Azure Python SDK 从 Blob 存储中读取表格数据集,如下所示:

df = Dataset.Tabular.from_delimited_files(path=[DataPath(ds, blobstore_dir + 'tabular_data.csv')],
                                          separator=',', header=True)

数据有四个日期时间列,其中一列读入没有问题,因为在某些情况下月日顺序不明确,但其他三列被错误地推断为“月日”而不是“日-月”。

读取数据时,我收到以下警告:

UserWarning: Ambiguous datetime formats inferred for columns ['Period Start', 'Period End', 'Extracted At'] are resolved as "month-day". Desired format can be specified by set_column_types.

我尝试如下设置列类型,并尝试了几种不同的格式,但最终得到的都是 NULL 代替所有值。

df = Dataset.Tabular.from_delimited_files(
        path=[DataPath(ds, blobstore_dir + 'tabular_data.csv')], separator=',', header=True,
        set_column_types={'Period Start': DataType.to_datetime("%d-%m-%Y %H:%M:%S"),
                          'Period End': DataType.to_datetime("%d-%m-%Y %H:%M:%S"),
                          'Extracted At': DataType.to_datetime("%d-%m-%Y %H:%M:%S")})

from_delimited_files() 的文档是 here

谁能告诉我如何强制 from_delimited_files() 将不明确的日期时间解析为日月,或者告诉我如何正确使用 set_column_types ?我通过插入具有明确日期时间的虚拟行暂时解决了这个问题。

最佳答案

您可以导入Pandas并使用date_parserto_datetime,您可以使用相应的格式转换日期时间。

date_parser=lambda x: pd.to_datetime(x, format='%m/%d/%Y %I:%M:%S %p'))

从文件中读取

pd.read_csv('testresult.csv', parse_dates=['TIME'], 
    date_parser=lambda x: pd.to_datetime(x, format='%m/%d/%Y %I:%M:%S %p'))

它还将格式传递给 date_parser参数,而不是让 pandas 尝试使用 infer_datetime_format 参数来猜测它。

引用here

关于Python Azure SDK - 使用 from_delimited_files() 从 blobstore 读取表格数据时推断出不正确的日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70586850/

相关文章:

python - 将 HTML 标记转换为 RTF 文档

python - 未创建文件,在 ubuntu 服务器 12.04 Cron Job 中

azure - 利用云服务对某个时间窗口内的实时统计数据进行聚合和分组,以触发通知

python - 执行 Azure 移动资源时是否有办法跳过错误?

Azure机器学习工作室: cannot create Datastore from Azure SQL Database

azure - 使用 azure python/java sdk 在 IoT 中心创建 azure IoT 设备

python - wxPython:更改标题的颜色

Azure AD - 即使配置为允许访问,对 Blazor 应用程序的跨租户访问也会被拒绝

visual-studio - Azure Function App = 在本地运行单个 Azure Function 进行调试

python - python中使用leastsq同时拟合数据