pandas_utils.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:betterself 作者: jeffshek 项目源码 文件源码
def force_start_end_data_to_dataframe(user, dataframe, start_date, end_date):
    assert type(dataframe) == pd.DataFrame

    # if dataframe contains any dates outside of start and end date ... exclude
    dataframe = dataframe[start_date:end_date].asfreq('D')

    index = pd.date_range(start=start_date, end=end_date, tz=user.pytz_timezone)

    # blank dataframe that we know for certain holds all the right dates
    dataframe_container = pd.DataFrame(index=index)

    # join the dataframe with an empty one that has all the right indices ... to return a dataframe with all the right
    # start and end dates
    normalized_dataframe = pd.DataFrame.join(dataframe_container, dataframe)

    # Pandas is like a fine edged sword, sometimes it cuts everything perfectly, other times you don't know it's
    # power and it claws at you and takes back the bamboo. For the record, problem is not the panda, but the trainer.
    assert dataframe_container.index.size == normalized_dataframe.index.size

    return normalized_dataframe
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号