首页 > 编程语言 >【python】pandas 时间序列转换

【python】pandas 时间序列转换

时间:2023-03-10 15:13:46浏览次数:33  
标签:03 00 04 python df pd time 序列 pandas

1. 时间戳-->时间

time_stamp = 1677895200000  # 2023-03-04 10:00:00
pd.to_datetime(time_stamp, unit='ms')  # Timestamp('2023-03-04 02:00:00')  utc时间
pd.to_datetime(time_stamp, unit='ms', origin='1970-01-01 08:00:00')  # Timestamp('2023-03-04 10:00:00')
pd.to_datetime(time_stamp, unit='ms', origin='1970-01-01 08:00:00').strftime('%F %T')  # '2023-03-04 10:00:00'

time_str = '2023-03-04 10:00:00'
pd.to_datetime(time_str, format='%Y-%m-%d %H:%M:%S')  # Timestamp('2023-03-04 10:00:00')
pd.to_datetime(time_str, format='%Y-%m-%d %H:%M:%S').strftime('%F')  # '2023-03-04'
pd.to_datetime(time_str, format='%Y-%m-%d %H:%M:%S').strftime('%F %T')  # ''2023-03-04 10:00:00'

2. 时间格式化

df['time']  # dtype: datetime64
df['time'][0]  # pandas._libs.tslibs.timestamps.Timestamp
df['time'].dt.strftime('%F %T')  # '2023-03-04 10:00:00'

3. 字符串-->时间

df['time']  # dtype: object
df['time'][0]  # str
pd.to_datetime(df['time'][0], format='%Y-%m-%d %H:%M:%S"')  # Timestamp('2023-03-03 08:02:17')
pd.to_datetime(df['time'], format='%Y-%m-%d %H:%M:%S')

4. 时间-->时间戳

df = pd.DataFrame({'date': ['2011-04-24 01:30:00.000']})
df['date'] = pd.to_datetime(df['date'])
df['date'].astype('int64')//1e9
# 0    1.303609e+09
# Name: date, dtype: float64
(df['date'].astype('int64')//1e9).astype('int')
# 0    1303608600
# Name: date, dtype: int32

标签:03,00,04,python,df,pd,time,序列,pandas
From: https://www.cnblogs.com/jessecheng/p/17203400.html

相关文章

  • Python - allure 报告使用汇总
    使用pytest做自动化测试过程中,关于allure报告的使用方法汇总pythonallure包使用allure命令行工具生成测试报告不启动服务$alluregenerate{allure_result}......
  • Python - else 语法总结
    else使用汇总。问题阅读别人代码,有点疑惑,精简后如下:defcode_example(arg=None):foriinrange(5):ifarg:breakelse:pr......
  • Python - 连接数据库
    python连接数据库操作pymysqlimportpymysqldefget_connect():connect=pymysql.connect( host="xxx.com",port=3306,user="test",......
  • Python - pandas 数据处理
    数据处理pandas数据读取pd.read_csv:csv/tsv/txt用逗号、tab分隔的纯文本文件pd.read_excel::微软xls或者xlsx文件pd.read_sql:mysql关系型数据库pd.rea......
  • Python -gdb 查看程序堆栈详情
    MacPython3.7https://www.modb.pro/db/454999安装#搜索仓库$brewsearchgdb#安装$brewinstallgdbError:[email protected]:thebottleneedstheAppleCom......
  • Python - 项目包管理 requirements
    python项目中的依赖库,可以创建一个requirements.txt文件来管理。allure-pytest=2.12.0pytest=7.2.0pytest-rerunfailures=10.3pytest-sugar=0.9.6#安装$pipinst......
  • Python - 获取调用者的函数名称
    def_is_page(self,locator):"""判断是否到达指定页面"""caller_name=traceback.extract_stack()[-2][2]is_page=self.ele_actions(locator).exists()......
  • Python - Crypto 导入失败问题解决记录
    python3.7Mac安装psycopg2$pipinstallpsycopg2...Error:pg_configexecutablenotfound....出现报错:Error:pg_configexecutablenotfound.解决参考:h......
  • Python - pip 报错记录
    #安装包出现错误$pipinstall-rrequirements.txt-ihttp://pypi.douban.com/simple/ERROR:Couldnotfindaversionthatsatisfiestherequirementpbr(from......
  • python 提取csv内容脚本
    目录python提取csv内容脚本python提取csv内容脚本提取csv的内容脚本,这里只是提取了单个csv文件的内容,也没有写入新的文件,也没有把数据处理成json,临时模版,比较简陋,方便......