不确定这是否是一个错误,但是我在将字符串转换为时间戳的同时使用自定义格式保留毫秒部分时遇到了麻烦。
当我尝试使用ISO-8601格式的字符串时,显式提供一种格式会导致解析器丢弃时间戳的毫秒部分。
我错了吗,对于大写字母S
,对于SimpleDateFormat格式来说,毫秒内的错误符号是错误的吗?
from pyspark.sql.types import StructType, StructField, StringType
from pyspark.sql.functions import col, to_timestamp
data = [['2018-07-20 15:11:22.023']]
schema = StructType([
StructField('datetime_string', StringType(), nullable=False)
])
df = spark.createDataFrame(data, schema=schema)
df2 = df.withColumn('timestamp', to_timestamp(col('datetime_string')))
print(df2.first().timestamp.microsecond) # 23000, as expected
df3 = df.withColumn('timestamp', to_timestamp(col('datetime_string'), format='yyyy-MM-dd HH:mm:ss.SSS'))
print(df3.first().timestamp.microsecond) # 0, unexpected!
环境信息:
通过“ apt-get install openjdk-8-jre”安装的“ java -version”的输出
openjdk版本“ 1.8.0_171”
OpenJDK运行时环境(内部版本1.8.0_171-8u171-b11-0ubuntu0.18.04.1-b11)
OpenJDK 64位服务器VM(内部版本25.171-b11,混合模式)