Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要以历元格式获取日期范围_Python_Pandas_Datetime_Time - Fatal编程技术网

Python 需要以历元格式获取日期范围

Python 需要以历元格式获取日期范围,python,pandas,datetime,time,Python,Pandas,Datetime,Time,需要以历元格式获取当前月份的范围(从开始到月底,两个变量,以毫秒为单位的历元格式)。怎么做?也许有人已经这样做了,并且可以提供帮助?来自熊猫文档:。将datetime转换为int64,然后除以转换单位 import pandas as pd def to_epoch(**kwargs): """returns: NumPy ndarray.""" stamps = pd.date_range(**kwargs) return stamps.view('int64') /

需要以历元格式获取当前月份的范围(从开始到月底,两个变量,以毫秒为单位的历元格式)。怎么做?也许有人已经这样做了,并且可以提供帮助?

来自熊猫文档:。将datetime转换为int64,然后除以转换单位

import pandas as pd

def to_epoch(**kwargs):
    """returns: NumPy ndarray."""
    stamps = pd.date_range(**kwargs)
    return stamps.view('int64') // pd.Timedelta(1, unit='s')

to_epoch(start='2017-04-01', end='2017-04-30')
array([1491004800, 1491091200, 1491177600, 1491264000, 1491350400,
       1491436800, 1491523200, 1491609600, 1491696000, 1491782400,
       1491868800, 1491955200, 1492041600, 1492128000, 1492214400,
       1492300800, 1492387200, 1492473600, 1492560000, 1492646400,
       1492732800, 1492819200, 1492905600, 1492992000, 1493078400,
       1493164800, 1493251200, 1493337600, 1493424000, 1493510400])

不是熊猫,但一般来说:

from datetime import datetime
import calendar

epoch = datetime.utcfromtimestamp(0)

def unix_time(dt):
    return (dt - epoch).total_seconds() * 1000.0


today = datetime.now()
lastDay = calendar.monthrange(today.year, today.month)[1]

firstDayOfMonth = today.replace(day=1)
lastDayofMonth = today.replace(day=lastDay)

firstEpoch = unix_time(firstDayOfMonth)
lastEpoch = unix_time(lastDayOfMonth)
或: