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

在 python 中有木有类似 mysql 中 hour(), day(), second()这种函数的库?

  •  
  •   ideaplat · Jul 13, 2016 · 3567 views
    This topic created in 3766 days ago, the information mentioned may be changed or developed.

    貌似 datetime,time,arrow 等都没有类似功能,或者有类似功能,我不知道。

    Supplement 1  ·  Jul 13, 2016

    import pandas as pd

    df = pd.read_csv("df.csv")

    print (df.head())

    dn = df.head()


    print (dn['fts'].hour)


    希望做类似的工作,谢谢

    18 replies  •  2016-07-14 10:50:25 +08:00
    xiahei
    xiahei
    Jul 13, 2016❤️1
    1
    shakespaces
    shakespaces
    Jul 13, 2016 via Android
    2
    datetime 有啊
    heaton_nobu
    heaton_nobu
    Jul 13, 2016
    3
    代码库里一般都是属性吧
    ideaplat
    ideaplat
    OP
    Jul 13, 2016
    4
    @xiahei 如何处理类似 dataframe 等格式的呢
    MyFaith
    MyFaith
    Jul 13, 2016 via iPhone
    5
    @xiahei 这是什么 terminal
    upczww
    upczww
    Jul 13, 2016 via Smartisan T1
    6
    @ideaplat 请学习 pandas 这个库
    upczww
    upczww
    Jul 13, 2016 via Smartisan T1
    7
    @MyFaith 感觉是 zsh
    xiahei
    xiahei
    Jul 13, 2016
    8
    @MyFaith Ipython.
    ideaplat
    ideaplat
    OP
    Jul 13, 2016
    9
    @upczww 我说的就是 pandas 的 dataframe 的 hour,day 如何处理?
    ideaplat
    ideaplat
    OP
    Jul 13, 2016
    10
    @MyFaith ipython
    princelai
    princelai
    Jul 13, 2016
    11
    import pandas as pd
    idx = pd.date_range('2015-12-01 9:30','2015-12-20 15:00',freq='B')
    idx.hour
    idx.minute
    idx[0].time().hour
    idx[0].minute

    idx[0].strftime('%Y-%m-%d')
    idx[0].strftime('%H-%M-%S')

    你还想怎么处理?
    est
    est
    Jul 13, 2016❤️1
    12
    没有。因为 mysql 里也没有支持中文括号的函数。
    ideaplat
    ideaplat
    OP
    Jul 13, 2016
    13
    @princelai
    import pandas as pd

    df = pd.read_csv("df.csv")


    dn = df.head()

    print (dn['fts'].hour)

    Unnamed: 0 f202001 fts h f202001_status
    0 0 1.0 2016-06-05 20:36:00 20.0 1.0
    1 25 0.0 2016-06-05 21:02:00 21.0 0.0
    2 29 1.0 2016-06-05 21:08:00 21.0 1.0
    3 35 0.0 2016-06-05 21:16:00 21.0 0.0
    4 356 1.0 2016-06-05 00:00:00 0.0 1.0


    AttributeError: 'Series' object has no attribute 'hour'
    ideaplat
    ideaplat
    OP
    Jul 13, 2016
    14
    @est 哈哈
    upczww
    upczww
    Jul 13, 2016 via Smartisan T1
    15
    @est 哈哈哈哈哈
    TimePPT
    TimePPT
    Jul 14, 2016
    16
    @est 手动点赞并感谢打赏!😂😂
    princelai
    princelai
    Jul 14, 2016❤️1
    17
    @ideaplat 要么你在 read_csv 的时候,指定 index 为时间那一列,然后用上面的方法,要么就这样

    from dateutil import parser
    tt = parser.parse(df['fts'][0])
    tt.hour
    自己做循环读然后转换,不过我觉得还是第一种方便
    ideaplat
    ideaplat
    OP
    Jul 14, 2016
    18
    @princelai 都不是很方便,谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1291 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 20:37 · PVG 04:37 · LAX 12:37 · JFK 15:37
    ♥ Do have faith in what you're doing.