"startPos and length must be the same type" 尝试在数据框中添加列时出错
"startPos and length must be the same type" error when trying to add a column in a dataframe
我有一个 .csv 文件,我正尝试使用 Python.
通过 Azure Databricks 对其进行修改
该文件有两列 timeOfMeasurement
,格式为 2021-01-04T07:07:45.098+0000
和 eventProcessedUtcTime
,格式为 2021-01-04T07:07:45.6768856Z
。我想添加这些字符串格式为日期时间的新列。
当我使用单个变量时,以下格式有效:
import datetime
# EventProcessedUtcTime
a = '2021-01-04T07:07:45.6768856Z'
x = datetime.datetime.strptime((a[:26]).strip(), '%Y-%m-%dT%H:%M:%S.%f')
print(x)
print(type(x))
# timeOfMeasurement
b = '2021-01-04T07:07:45.098+0000'
y = datetime.datetime.strptime((b[:23]).strip(), '%Y-%m-%dT%H:%M:%S.%f')
#y = (b[:23]).strip()
print(y)
print(type(y))
并创建以下输出:
2021-01-04 07:07:45.676885
<class 'datetime.datetime'>
2021-01-04 07:07:45.098000
<class 'datetime.datetime'>
但是当我尝试为整个数据帧实现它时
import datetime
df = spark.read.format('csv').options(header='true', inferSchema='true').load('/mnt/data/values.csv')
df_adjusted = (df.withColumn("timeOfMeasurementDatetime", datetime.datetime.strptime((df.timeOfMeasurement[:23]).strip(), '%Y-%m-%dT%H:%M:%S.%f'))
我收到一条错误消息:
TypeError: startPos and length must be the same type. Got <class 'NoneType'> and <class 'int'>, respectively.
我不知道问题出在哪里,因为我对整个主题完全陌生,如果有人能帮助我,我会非常高兴。
您需要为此使用 PySpark 函数,而不是使用 Python 的内置函数。您有以下可能性:
- 如果您的字符串是 ISO8601 时间格式,那么您可以在相应的列上执行
.cast('timestamp')
:
>>> df = spark.createDataFrame([('2021-01-04T07:07:45.098+0000',
'2021-01-04T07:07:45.6768856Z')],
['ts1','ts2'])
>>> df.show(truncate=False)
+----------------------------+----------------------------+
|ts1 |ts2 |
+----------------------------+----------------------------+
|2021-01-04T07:07:45.098+0000|2021-01-04T07:07:45.6768856Z|
+----------------------------+----------------------------+
>>> df.select(df.ts1.cast('timestamp'), df.ts2.cast('timestamp')).show(truncate=False)
+-----------------------+--------------------------+
|ts1 |ts2 |
+-----------------------+--------------------------+
|2021-01-04 08:07:45.098|2021-01-04 08:07:45.676885|
+-----------------------+--------------------------+
- 如果您的字符串有一些自定义格式,您可以使用 to_timestamp 等内置函数并提供格式字符串。
我有一个 .csv 文件,我正尝试使用 Python.
通过 Azure Databricks 对其进行修改该文件有两列 timeOfMeasurement
,格式为 2021-01-04T07:07:45.098+0000
和 eventProcessedUtcTime
,格式为 2021-01-04T07:07:45.6768856Z
。我想添加这些字符串格式为日期时间的新列。
当我使用单个变量时,以下格式有效:
import datetime
# EventProcessedUtcTime
a = '2021-01-04T07:07:45.6768856Z'
x = datetime.datetime.strptime((a[:26]).strip(), '%Y-%m-%dT%H:%M:%S.%f')
print(x)
print(type(x))
# timeOfMeasurement
b = '2021-01-04T07:07:45.098+0000'
y = datetime.datetime.strptime((b[:23]).strip(), '%Y-%m-%dT%H:%M:%S.%f')
#y = (b[:23]).strip()
print(y)
print(type(y))
并创建以下输出:
2021-01-04 07:07:45.676885
<class 'datetime.datetime'>
2021-01-04 07:07:45.098000
<class 'datetime.datetime'>
但是当我尝试为整个数据帧实现它时
import datetime
df = spark.read.format('csv').options(header='true', inferSchema='true').load('/mnt/data/values.csv')
df_adjusted = (df.withColumn("timeOfMeasurementDatetime", datetime.datetime.strptime((df.timeOfMeasurement[:23]).strip(), '%Y-%m-%dT%H:%M:%S.%f'))
我收到一条错误消息:
TypeError: startPos and length must be the same type. Got <class 'NoneType'> and <class 'int'>, respectively.
我不知道问题出在哪里,因为我对整个主题完全陌生,如果有人能帮助我,我会非常高兴。
您需要为此使用 PySpark 函数,而不是使用 Python 的内置函数。您有以下可能性:
- 如果您的字符串是 ISO8601 时间格式,那么您可以在相应的列上执行
.cast('timestamp')
:
>>> df = spark.createDataFrame([('2021-01-04T07:07:45.098+0000',
'2021-01-04T07:07:45.6768856Z')],
['ts1','ts2'])
>>> df.show(truncate=False)
+----------------------------+----------------------------+
|ts1 |ts2 |
+----------------------------+----------------------------+
|2021-01-04T07:07:45.098+0000|2021-01-04T07:07:45.6768856Z|
+----------------------------+----------------------------+
>>> df.select(df.ts1.cast('timestamp'), df.ts2.cast('timestamp')).show(truncate=False)
+-----------------------+--------------------------+
|ts1 |ts2 |
+-----------------------+--------------------------+
|2021-01-04 08:07:45.098|2021-01-04 08:07:45.676885|
+-----------------------+--------------------------+
- 如果您的字符串有一些自定义格式,您可以使用 to_timestamp 等内置函数并提供格式字符串。