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
yellowtail
V2EX  ›  Python

dataframe 的 apply, lambda,加自定函数报错。

  •  
  •   yellowtail · 2020-08-30 03:34:05 +08:00 · 1287 次点击
    这是一个创建于 1334 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def fuc_time(x,a):
    if ((x - x.replace( hour=0,minute=0,second=0 )-datetime.timedelta(minutes =1))%datetime.timedelta(minutes =a)) == datetime.timedelta(minutes =0):
    return True
    else:
    return False

    df.groupby(df['trade_date'].apply(lambda x:x.replace( hour=0,minute=0,second=0 ))).apply(lambda x:fuc_time(x,5))

    报错 replace() got an unexpected keyword argument 'hour'


    提问题然后去睡觉了。。。
    3 条回复    2020-08-30 22:45:08 +08:00
    widewing
        1
    widewing  
       2020-08-30 03:54:31 +08:00 via Android
    df['trade_date']类型不是日期咯
    volvo007
        2
    volvo007  
       2020-08-30 11:07:46 +08:00
    看起来是想按“天”来分类

    对于时间戳,Series 自己的 .dt 系列方法就可以。比如 df[column].dt.date 或者 df[column].dt.floor('d') (这个 floor 类似 “向下取整”,同理还有 .dt.ceil 和 .dt.round 方法),不需要转为 hour=0, minute=0, second=0 这么麻烦

    转换失败的话应该是同 1L 的情况
    yellowtail
        3
    yellowtail  
    OP
       2020-08-30 22:45:08 +08:00
    @widewing df['trade_date']类型是日期,错误在第二个 lambda 里的 x 的对象是 df 类型 需要再加一个 apply 实现对 df 里头的列再次 lambda... 然后得到一个 series 再把 series 插入就可以了。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3548 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:52 · PVG 18:52 · LAX 03:52 · JFK 06:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.