1
インシデントと特定のタイムスタンプを持つテーブルがあります。私は、Pyspark 2.0 APIを使って渡された日数を計算するのに苦労しています。タイムスタンプは、別のフォーマットに従ったとき、私は、私は(何も働いていない)次のことを試してみましたPyspark:2つの日付の差異(キャストタイムスタンプタイプ、Datediff)
+-------------------+------------------------+----------+--------------+
| first_booking_date|first_booking_date_clean| today |customer_since|
+-------------------+------------------------+----------+--------------+
|02-06-2011 20:52:04| 02-06-2011|02-06-2011| null|
|03-06-2004 18:15:10| 03-06-2004|02-06-2011| null|
に
(YYYY-MM-DD)同じことを行うために管理: - 文字列操作で抽出日とDATEDIFF を使用 - にキャストタイムスタンプを抽出してからdd:MM:yy( - > result null)を抽出する - pysparkコマンドをSQLで追加変換するよりも好きです。助けていただきありがとうございます。
編集:ここでは動作しませんでした例です。
import datetime
today = datetime.date(2011,2,1)
today = "02-06-2011"
first_bookings = first_bookings.withColumn("today",F.lit(today))
first_bookings = first_bookings.withColumn("first_booking_date_clean",F.substring(first_bookings.first_booking_date, 0, 10))
first_bookings = first_bookings.withColumn("customer_since",F.datediff(first_bookings.today,first_bookings.first_booking_date_clean))
必要に応じてさらに詳しい情報を追加してください! –
https://stackoverflow.com/a/36985244/4219202同様の質問に対するこの回答を確認してください。あなたの場合、timeFmtはカラムfirst_booking_date_cleanと今日の – Zephro
に対して "dd-MM-yyyy"となります。[文字列フォーマットで2つのdatetimeカラムを減算することによって継続時間を計算する](https://stackoverflow.com/questions/30283415/calculating -duration-by-subtracting-two-datetime-columns-in-string-format) – sdikby