- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含三列的 PySpark 数据框。前两列以数组作为元素,最后一列给出最后一列的数组长度。以下是 PySpark 数据框:
+---------------------+---------------------+-----+
| c1| c2|lenc2|
+---------------------+---------------------+-----+
|[2017-02-14 00:00:00]|[2017-02-24 00:00:00]| 1|
|[2017-01-16 00:00:00]| []| 0|
+---------------------+---------------------+-----+
数组包含时间戳数据类型。 lenc2
列表示 c1
列中数组的长度。对于 lenc2==0
的所有行,列 c1
只有一个(时间戳)元素。
对于 lenc2==0
的所有行,我想从列 c1
中的数组中获取时间戳,添加 5 天并将其放入在 c2
行的数组内。我怎样才能做到这一点?
这是预期输出的示例:
+---------------------+---------------------+-----+
| c1| c2|lenc2|
+---------------------+---------------------+-----+
|[2017-02-14 00:00:00]|[2017-02-24 00:00:00]| 1|
|[2017-01-16 00:00:00]|[2017-01-21 00:00:00]| 0|
+---------------------+---------------------+-----+
以下是我迄今为止尝试过的:
df2 = df1.withColumn(
"c2",
F.when(F.col("lenc2") == 0, F.array_union(F.col("c1"), F.col("c2"))).otherwise(
F.col("c2")
),
)
最佳答案
您的 when(...).otherwise(...)
已经正确。
鉴于您似乎对亚秒精度不感兴趣,您可以将时间戳转换为自 Unix 纪元以来的秒数,并添加 5 天的秒数,然后转换回时间戳:
from datetime import datetime
from pyspark.sql.functions import *
one_sec_before_leap_time = datetime(2016, 12, 31, 23, 59, 59)
seconds_in_a_day = 24 * 3600
df = spark.createDataFrame([
([one_sec_before_leap_time], [datetime.now()], 1),
([one_sec_before_leap_time], [], 0),
],
schema=("c1", "c2", "lenc2"))
def add_seconds_to_timestamp(ts_col, seconds_col):
return to_timestamp(unix_timestamp(ts_col) + seconds_col)
df2 = df.withColumn("c2",
when(col("lenc2") == 0,
array(
add_seconds_to_timestamp(
col("c1").getItem(0),
lit(5 * seconds_in_a_day))))
.otherwise(col("c2")))
df2.show(truncate=False)
# +---------------------+----------------------------+-----+
# |c1 |c2 |lenc2|
# +---------------------+----------------------------+-----+
# |[2016-12-31 23:59:59]|[2019-12-07 16:58:32.864176]|1 |
# |[2016-12-31 23:59:59]|[2017-01-05 23:59:59] |0 |
# +---------------------+----------------------------+-----+
请注意,当您必须考虑夏令时时,这很可能会给您带来奇怪的结果。最好用 UTC 表示所有内容,并且仅在输入和输出处进行从 UTC 时间戳到以本地时区表示的时间的适当转换。基本上类似于 Unicode 三明治。
此外,这没有考虑leap seconds ,如上所示(2016 年还有一秒钟,使得 2016-12-31T12:59:60Z 在技术上有效)。然而,闰秒是出了名的困难,因为它没有确切的公式(但是 - 谁知道,也许有一天我们可以模拟地质和气候事件?)。
关于python - 在 PySpark Dataframe 中向数组内的元素添加天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224508/
我的目标是获取购买给定产品所需的平均天数。如果 Product_A 在给定时间段内购买了 3 次(“2012-12-01”、“2012-12-05”、“2012-12-10”),那么我们的平均订单间隔
我在计算利息天数时有一个有趣的错误。我每天检查并检查今天是哪一天(1 到 31)。现在我发现了一个问题:10月计数不正常。这意味着 27 号是 26 号,或者 29 号是 28 号。这是一个众所周知的
你好我一直在做一些程序,我的程序是获取一年中过去的天数。现在,当我尝试运行时,输出结果为“4438232”。 例如,如果用户输入 (mm-dd-yy) 3-18-2013,则该年经过的扩孔天数为 77
我看到此问题已针对 Java 得到解答, JavaScript , 和 PHP ,但不是 C#。那么,如何在 C# 中计算两个日期之间的天数? 最佳答案 假设 StartDate 和 EndDate
QQ每天抽超级会员成长值、天数 非必中 手Q打开链接进入活动,下拉页面随便分享一下领取抽奖机会,亲测3点成长值! 活动地址:https://sourl.cn/37CZad 手Q扫码:
是否有任何内置函数可以找出时间戳之间的天数、添加天数或查找时间戳之间的月数?目前,我将日期作为字符串存储在我的文档中。 例如,如果我这样做: return (DATE_TIMESTAMP("2014-
我无法弄清楚如何为以下场景编写公式。我需要根据另一列计算日期,但需要根据原始列的星期几添加天数。 If day of week equals 'Mon/Wed/Fri/Sat' then add 5
我有两个系列的 Pandas 约会时间。我为数据框中的每一行减去它们,并添加一列以获取两个日期时间之间的时间增量。随后我想使用该时间增量来扩展另一个功能。所以我想对那个时间增量进行一些划分。没有骰子。
这个问题已经有答案了: "cannot find symbol: method" but the method is declared (3 个回答) How to add one day to a
我的代码是这样的,但第三行出错 mm/dd/yyyy 我的数据库日期格式如下: SELECT evep year(date='MM/dd/yyyy'), month(da
我有两个 jQuery UI 日期选择器,当它们都选择了日期时,我希望这些日期之间的差异显示在单独的 input[type="text"] 一旦选择了第二个日期。 此外,理想情况下,我希望我的计数减去
我有两列:rental_date 和 actual_retdate。我需要找到 actual_retdate 和 rental_date 之间的天数。 actual_retdate 在某些情况下可以为
运行 MySql 和 Yii 我需要更改当前查找当月记录的关系查询以查找过去 45 天的所有记录。 这是我当前的查询: 'itemCount' => array(self::STAT, 'It
我想创建一个新列,它将显示两个日期之间的时间增量(以天为单位),如以下 pandas 数据框所示: >>> hg[['not inc','date']] not inc
我有: 年份数字(可以是任何年份) 月份数(从一月到十二月) 周数(第 1、2、3、4、最后) 工作日(周日、周一、周二、周三、周四、周五、周六) 我需要获取天数 [从 1 到 ~31] - "YYY
我需要为一些项目倒数 90 天、120 天和 160 天。我将如何编码?我一直在寻找 java 代码,但是在我为 android 开发时会出现错误。 我需要用今天的日期减去 x 天,然后将结果显示在屏
我的要求是计算给定的两个日期之间的天数,不包括星期六和星期日。 示例: Start date - 10/09/15 and End date 18/09/15 Result: 7 日期采用 DD/MM
我想比较我的 Android 应用程序的两个日期,但我遇到了一个非常奇怪的问题。 例如: 如果我将 回到过去 日期设置为 127 天前: this.dateEvent = System.current
如何返回所提供日期的 CumSum 天数? import pandas as pd df = pd.DataFrame({ 'date': ['2019-01-01','2019-01-03'
例如,我可以使用以下代码获取当前日期以显示在 DatePicker 对话框中。 final Calendar c = Calendar.getInstance(); mYear = c
我是一名优秀的程序员,十分优秀!