推荐学习书目
› 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
0.01D
V2EX  ›  Python

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

  •  
  •   particlec · Feb 8, 2023 · 2586 views
    This topic created in 1337 days ago, the information mentioned may be changed or developed.
    我原来的数据是这样子的 :

    日期 两年收益率 五年收益率 十年收益率
    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 replies  •  2023-02-08 16:05:59 +08:00
    HashV2
        1
    HashV2  
       Feb 8, 2023   ❤️ 1
    先转成字符串不行吗

    XXX['日期'] = XXX['日期'].astype('str')
    XXX.to_json(orient="index", force_ascii=False)
    particlec
        2
    particlec  
    OP
       Feb 8, 2023
    可以了,欸谢谢
    thinkershare
        3
    thinkershare  
       Feb 8, 2023   ❤️ 2
    这个问题非常恶心,官方有人提建议 date_unit 添加 D 参数,但是也一直没有啥进展,现在只能恶心的提前转换一下了。另外其实这么搞也不符合 JSON 规范,我估计官方永远都不会支持。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   803 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 19:04 · PVG 03:04 · LAX 12:04 · JFK 15:04
    ♥ Do have faith in what you're doing.