【问题标题】:Adding pd.series of minutes/seconds to a fixed datetime pandas将 pd.series 的分钟/秒添加到固定的日期时间 pandas
【发布时间】:2019-02-15 18:37:48
【问题描述】:

我有一个固定的单一日期时间 (datetime.datetime(2019, 2, 5, 16, 40, 19) ) 和一系列增加的 %H:%M:%S。 pd 系列不附加日期,仅代表各种时间量。我希望该系列是固定日期+时间量。所以,如果 pd 系列是:

00:01:21
00:01:23
00:01:25
00:01:30

我希望该系列的第一个条目是 2 月 5 日之后 1 分 21 秒,下午 4:40:19,第二个条目是 1 分 23 秒之后,等等

有什么好的 pandas/pythonic 方法可以做到这一点?

【问题讨论】:

  • 不是最熊猫的做法,你自己实现了什么吗?发生了什么,错误?

标签: python pandas datetime


【解决方案1】:

我假设您的系列是文本格式,因为您没有指定。如果是,实际上已经在时间增量中,您不需要第一步。

# Create some test data
my_series = pd.Series(['00:01:21',
                '00:01:23',
                '00:01:25',
                '00:01:30'])

# Create series of time deltas
my_delta = pd.to_timedelta(my_series)

现在您可以根据原始数据创建一系列新日期

 # Create variable with the date to add
 my_date = datetime.datetime(2019, 2, 5, 16, 40, 19)

 # Add the two together
 result = my_date + my_delta

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 2016-02-19
    • 2016-01-07
    • 2017-06-21
    • 1970-01-01
    • 2021-05-21
    • 2016-07-10
    • 2017-03-28
    相关资源
    最近更新 更多