pyspark
データフレームがあります。このデータフレームにtest_time
は、string
データ型の列があります。
>>> df
DataFrame[test_time: string]
df.show()
+-------------------+
| test_time|
+-------------------+
|2017-03-12 02:41:06|
|2017-03-12 02:43:52|
|2017-03-12 02:56:32|
|2017-03-12 03:16:23|
|2017-03-12 03:17:15|
|2017-03-12 03:22:19|
|2017-03-12 03:52:19|
|2017-03-12 04:03:21|
+-------------------+
このtest_time
列をからstring
に変換したいtimestamp
私は以下のようにしました
from pyspark.sql import functions as F
df1 = df.withColumn('convert_test', F.unix_timestamp('test_time', "yyyy-MM-dd hh:mm:ss").cast('timestamp'))
>>> df1
DataFrame[test_time: string, convert_test: timestamp]
df1.show()
+-------------------+--------------------+
| test_time| convert_test|
+-------------------+--------------------+
|2017-03-12 02:41:06|2017-03-12 03:41:...|
|2017-03-12 02:43:52|2017-03-12 03:43:...|
|2017-03-12 02:56:32|2017-03-12 03:56:...|
|2017-03-12 03:16:23|2017-03-12 03:16:...|
|2017-03-12 03:17:15|2017-03-12 03:17:...|
|2017-03-12 03:22:19|2017-03-12 03:22:...|
|2017-03-12 03:52:19|2017-03-12 03:52:...|
|2017-03-12 04:03:21|2017-03-12 04:03:...|
+-------------------+--------------------+
ご覧のHours
とおり、行は異なります1-3
。
FYI
私のタイムゾーンはでPST
あり、行1-3
はday light savings
時間中のタイミングです。
正しい変換を行うにはどうすればよいですか。
私は正しい出力を得ています unix_timestamp()
val dataframe = Seq(
("2017-03-12 02:41:06"),
("2017-03-12 02:43:52"),
("2017-03-12 02:56:32"),
("2017-03-12 03:16:23"),
("2017-03-12 03:17:15"),
("2017-03-12 03:22:19"),
("2017-03-12 03:52:19"),
("2017-03-12 04:03:21")
).toDF("test_time")
dataframe.withColumn("convert_test", unix_timestamp($"test_time", "yyyy-MM-dd hh:mm:ss").cast("timestamp")).show()
出力:
+-------------------+--------------------+
| test_time| convert_test|
+-------------------+--------------------+
|2017-03-12 02:41:06|2017-03-12 02:41:...|
|2017-03-12 02:43:52|2017-03-12 02:43:...|
|2017-03-12 02:56:32|2017-03-12 02:56:...|
|2017-03-12 03:16:23|2017-03-12 03:16:...|
|2017-03-12 03:17:15|2017-03-12 03:17:...|
|2017-03-12 03:22:19|2017-03-12 03:22:...|
|2017-03-12 03:52:19|2017-03-12 03:52:...|
|2017-03-12 04:03:21|2017-03-12 04:03:...|
+-------------------+--------------------+
あなたはのような機能を使用することができるとは異なるタイムゾーンにしている場合from_utc_timestamp()
やto_utc_timestamp()
、タイムスタンプを変換します。
これがお役に立てば幸いです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加