欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python按天實(shí)現(xiàn)生成時(shí)間范圍序列的方法詳解

 更新時(shí)間:2022年11月16日 11:24:22   作者:ponponon  
有的時(shí)候我們希望生成一段時(shí)間返回,比如從?2022-01-01?00:00:00?后面的?10?天,這么?10?個(gè)?datetime?對(duì)象,但是我們又不想自己去計(jì)算哪些月有30天哪些月有31天。所以本文將用Python實(shí)現(xiàn)按天自動(dòng)生成時(shí)間范圍序列,需要的可以參考一下

有的時(shí)候我們希望生成一段時(shí)間返回,比如從 2022-01-01 00:00:00 后面的 10 天,這么 10 個(gè) datetime 對(duì)象,但是我們又不想自己去計(jì)算哪些月有30天哪些月有31天。

使用 timedelta

datetime 中包含了 timedelta ,可以用來實(shí)現(xiàn)這個(gè)功能

from datetime import datetime, timedelta, timezone
from pydantic.datetime_parse import parse_datetime
from loguru import logger


SECOND: int = 1
MINUTE: int = SECOND*60
HOUR: int = MINUTE*60
DAY: int = HOUR*24
WEEK: int = DAY*7
MONTH: int = DAY*30


def get_utc_now_timestamp(with_tzinfo: bool = True) -> datetime:
    """ https://blog.csdn.net/ball4022/article/details/101670024 """
    if not with_tzinfo:
        return datetime.utcnow()
    return datetime.utcnow().replace(tzinfo=timezone.utc)


def timedelta_seconds(start_time: datetime, end_time: datetime = None) -> int:
    """ 返回兩個(gè)時(shí)間相差的秒數(shù) """
    if not end_time:
        end_time = get_utc_now_timestamp()

    return int((end_time - start_time).total_seconds())


def custom_timestamp(base_timestamp: datetime, seconds: int, reduce=False):
    return base_timestamp + timedelta(seconds=seconds) \
        if not reduce \
        else base_timestamp - timedelta(seconds=seconds)


start_datetime = parse_datetime('2022-02-27 00:00:00')
data = [
    dt
    for dt in [
        custom_timestamp(start_datetime, DAY*i) for i in range(10)
    ]
]
logger.debug(data)

輸出如下:

╰─?  python -u "/Users/ponponon/Desktop/code/me/ideaboom/main.py"
2022-11-15 15:18:37.653 | DEBUG    | __main__:<module>:67 - [datetime.datetime(2022, 2, 27, 0, 0), datetime.datetime(2022, 2, 28, 0, 0), datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 3, 2, 0, 0), datetime.datetime(2022, 3, 3, 0, 0), datetime.datetime(2022, 3, 4, 0, 0), datetime.datetime(2022, 3, 5, 0, 0), datetime.datetime(2022, 3, 6, 0, 0), datetime.datetime(2022, 3, 7, 0, 0), datetime.datetime(2022, 3, 8, 0, 0)]

使用 arrow 這個(gè)第三方庫

import arrow
from loguru import logger
from pydantic.datetime_parse import parse_datetime

for crawl_date in arrow.Arrow.range('day', parse_datetime('2022-02-27 00:00:00'), parse_datetime('2022-03-10 00:00:00')):
    logger.debug(crawl_date.datetime)

輸出如下:

╰─?  python -u "/Users/ponponon/Desktop/code/me/ideaboom/datetime_arrow_range.py"
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-02-27 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-02-28 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-01 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-02 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-03 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-04 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-05 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-06 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-07 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-08 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-09 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__:<module>:6 - 2022-03-10 00:00:00+00:00

補(bǔ)充

當(dāng)然,Python還有很多生成不同要求的時(shí)間序列的方法,下面小編為大家整理了一些,希望對(duì)大家有所幫助

生成不同間隔的時(shí)間序列

import pandas as pd
import numpy as np
import datetime as dt

# 從2022-07-01開始,間隔3天,生成10條 時(shí)間數(shù)據(jù)
rng = pd.date_range('2022-07-01', periods = 10, freq = '3D')
print(rng)
print("#####################")

# 指定開始時(shí)間,結(jié)束時(shí)間  以及頻率
data=pd.date_range('2022-01-01','2023-01-01',freq='M')
print(data)
print("#####################")

# 從2022-01-01開始,間隔1天,生成20條 時(shí)間數(shù)據(jù)
time=pd.Series(np.random.randn(20),
           index=pd.date_range(dt.datetime(2022,1,1),periods=20))
print(time)
print("#####################")

# 不規(guī)則的時(shí)間間隔
p1 = pd.period_range('2022-01-01 10:10', freq = '25H', periods = 10)
print(p1)
print("######################################")

# 指定索引
rng = pd.date_range('2022 Jul 1', periods = 10, freq = 'D')
print(pd.Series(range(len(rng)), index = rng))
print("######################################")

測試記錄:

DatetimeIndex(['2022-07-01', '2022-07-04', '2022-07-07', '2022-07-10',
               '2022-07-13', '2022-07-16', '2022-07-19', '2022-07-22',
               '2022-07-25', '2022-07-28'],
              dtype='datetime64[ns]', freq='3D')
#####################
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
               '2022-09-30', '2022-10-31', '2022-11-30', '2022-12-31'],
              dtype='datetime64[ns]', freq='M')
#####################
2022-01-01   -0.957412
2022-01-02   -0.333720
2022-01-03    1.079960
2022-01-04    0.050675
2022-01-05    0.270313
2022-01-06   -0.222715
2022-01-07   -0.560258
2022-01-08    1.009430
2022-01-09   -0.678157
2022-01-10    0.213557
2022-01-11   -0.720791
2022-01-12    0.332096
2022-01-13   -0.986449
2022-01-14   -0.357303
2022-01-15   -0.559618
2022-01-16    0.480281
2022-01-17   -0.443998
2022-01-18    1.541631
2022-01-19   -0.094559
2022-01-20    1.875012
Freq: D, dtype: float64
#####################
PeriodIndex(['2022-01-01 10:00', '2022-01-02 11:00', '2022-01-03 12:00',
             '2022-01-04 13:00', '2022-01-05 14:00', '2022-01-06 15:00',
             '2022-01-07 16:00', '2022-01-08 17:00', '2022-01-09 18:00',
             '2022-01-10 19:00'],
            dtype='period[25H]', freq='25H')
######################################
2022-07-01    0
2022-07-02    1
2022-07-03    2
2022-07-04    3
2022-07-05    4
2022-07-06    5
2022-07-07    6
2022-07-08    7
2022-07-09    8
2022-07-10    9
Freq: D, dtype: int64
######################################

截?cái)鄷r(shí)間段

import pandas as pd
import numpy as np
import datetime as dt

# 從2022-01-01開始,間隔1天,生成20條 時(shí)間數(shù)據(jù)
time=pd.Series(np.random.randn(20),
           index=pd.date_range(dt.datetime(2022,1,1),periods=20))
print(time)
print("#####################")

# 只輸出2022-01-10 之后的數(shù)據(jù)
print(time.truncate(before='2022-1-10'))
print("#####################")

# 只輸出2022-01-10 之后的數(shù)據(jù)
print(time.truncate(after='2022-1-10'))
print("#####################")

# 輸出區(qū)間段
print(time['2022-01-15':'2022-01-20'])
print("#####################")

測試記錄:

2022-01-01   -0.203552
2022-01-02   -1.035483
2022-01-03    0.252587
2022-01-04   -1.046993
2022-01-05    0.152435
2022-01-06   -0.534518
2022-01-07    0.770170
2022-01-08   -0.038129
2022-01-09    0.531485
2022-01-10    0.499937
2022-01-11    0.815295
2022-01-12    2.315740
2022-01-13   -0.443379
2022-01-14   -0.689247
2022-01-15    0.667250
2022-01-16   -2.067246
2022-01-17   -0.105151
2022-01-18   -0.420562
2022-01-19    1.012943
2022-01-20    0.509710
Freq: D, dtype: float64
#####################
2022-01-10    0.499937
2022-01-11    0.815295
2022-01-12    2.315740
2022-01-13   -0.443379
2022-01-14   -0.689247
2022-01-15    0.667250
2022-01-16   -2.067246
2022-01-17   -0.105151
2022-01-18   -0.420562
2022-01-19    1.012943
2022-01-20    0.509710
Freq: D, dtype: float64
#####################
2022-01-01   -0.203552
2022-01-02   -1.035483
2022-01-03    0.252587
2022-01-04   -1.046993
2022-01-05    0.152435
2022-01-06   -0.534518
2022-01-07    0.770170
2022-01-08   -0.038129
2022-01-09    0.531485
2022-01-10    0.499937
Freq: D, dtype: float64
#####################
2022-01-15    0.667250
2022-01-16   -2.067246
2022-01-17   -0.105151
2022-01-18   -0.420562
2022-01-19    1.012943
2022-01-20    0.509710
Freq: D, dtype: float64
#####################

到此這篇關(guān)于Python按天實(shí)現(xiàn)生成時(shí)間范圍序列的方法詳解的文章就介紹到這了,更多相關(guān)Python時(shí)間范圍序列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3 io文本及原始流I/O工具用法詳解

    Python3 io文本及原始流I/O工具用法詳解

    這篇文章主要介紹了Python3 io文本及原始流I/O工具用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Python模塊future用法原理詳解

    Python模塊future用法原理詳解

    這篇文章主要介紹了Python模塊future用法原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Pyqt5設(shè)置返回鍵和跳轉(zhuǎn)頁面鍵的示例代碼

    Pyqt5設(shè)置返回鍵和跳轉(zhuǎn)頁面鍵的示例代碼

    這篇文章主要介紹了Pyqt5設(shè)置返回鍵和跳轉(zhuǎn)頁面鍵的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 使用Docker制作Python環(huán)境連接Oracle鏡像

    使用Docker制作Python環(huán)境連接Oracle鏡像

    這篇文章主要為大家介紹了使用Docker制作Python環(huán)境連接Oracle鏡像示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • python xlsxwriter模塊的使用

    python xlsxwriter模塊的使用

    這篇文章主要介紹了python xlsxwriter模塊的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Tensorflow 定義變量,函數(shù),數(shù)值計(jì)算等名字的更新方式

    Tensorflow 定義變量,函數(shù),數(shù)值計(jì)算等名字的更新方式

    今天小編就為大家分享一篇Tensorflow 定義變量,函數(shù),數(shù)值計(jì)算等名字的更新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python中實(shí)現(xiàn)php的var_dump函數(shù)功能

    python中實(shí)現(xiàn)php的var_dump函數(shù)功能

    這篇文章主要介紹了python中實(shí)現(xiàn)php的var_dump函數(shù)功能,var_dump函數(shù)在PHP中調(diào)試時(shí)非常實(shí)用,本文介紹在Python中實(shí)現(xiàn)這個(gè)函數(shù),需要的朋友可以參考下
    2015-01-01
  • python xml模塊的簡單使用

    python xml模塊的簡單使用

    這篇文章主要介紹了python xml模塊的簡單使用,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • python實(shí)現(xiàn)pptx批量向PPT中插入圖片

    python實(shí)現(xiàn)pptx批量向PPT中插入圖片

    大家好,本篇文章主要講的是python實(shí)現(xiàn)pptx批量向PPT中插入圖片,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • 用python + hadoop streaming 分布式編程(一) -- 原理介紹,樣例程序與本地調(diào)試

    用python + hadoop streaming 分布式編程(一) -- 原理介紹,樣例程序與本地調(diào)試

    Hadoop 是一個(gè)實(shí)現(xiàn)了 MapReduce 計(jì)算模型的開源分布式并行編程框架,借助于 Hadoop, 程序員可以輕松地編寫分布式并行程序,將其運(yùn)行于計(jì)算機(jī)集群上,完成海量數(shù)據(jù)的計(jì)算。
    2014-07-07

最新評(píng)論