V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
particlec
V2EX  ›  Python

[ Python 求助] Python 时间转化问题?

  •  
  •   particlec · 2023-02-08 15:34:18 +08:00 · 1582 次点击
    这是一个创建于 455 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我原来的数据是这样子的 :

    日期 两年收益率 五年收益率 十年收益率
    1 2023-01-30 2.435 2.723 2.913
    2 2023-01-31 2.419 2.703 2.897
    ...........

    然后我想序列号输出
    XXX.to_json(orient="index", force_ascii=False)
    变成下面这样子:
    {"日期":1675641600000,"中国国债收益率 5 年":2.7054 ,......}


    然后我想去掉时间戳:
    XXX.to_json(orient="index", force_ascii=False,date_format='iso', date_unit='s')
    变成了下面样子:
    {"日期":"2023-02-06T00:00:00","中国国债收益率 5 年":2.7054}

    但是我不需要秒级单位,我只需要到天 2023-02-06 ,要怎么才能去掉秒?求助各位大佬
    3 条回复    2023-02-08 16:05:59 +08:00
    HashV2
        1
    HashV2  
       2023-02-08 15:52:10 +08:00   ❤️ 1
    先转成字符串不行吗

    XXX['日期'] = XXX['日期'].astype('str')
    XXX.to_json(orient="index", force_ascii=False)
    particlec
        2
    particlec  
    OP
       2023-02-08 15:54:07 +08:00
    可以了,欸谢谢
    thinkershare
        3
    thinkershare  
       2023-02-08 16:05:59 +08:00   ❤️ 2
    这个问题非常恶心,官方有人提建议 date_unit 添加 D 参数,但是也一直没有啥进展,现在只能恶心的提前转换一下了。另外其实这么搞也不符合 JSON 规范,我估计官方永远都不会支持。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5740 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 449ms · UTC 08:57 · PVG 16:57 · LAX 01:57 · JFK 04:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.