gpt4 book ai didi

python - Pandas - 巨大的内存消耗

转载 作者:行者123 更新时间:2023-12-01 04:44:03 25 4
gpt4 key购买 nike

从包含约 1500 万行(占用约 250 MB)的 pickle 加载数据帧后,我对其执行了一些搜索操作,然后就地删除了一些行。在这些操作期间,内存使用量猛增至 5 GB,有时甚至 7 GB,这很烦人,因为交换(我的笔记本电脑只有 8 GB 内存)。

关键是当操作完成时(即执行下面代码中的最后两行时),不会释放此内存。所以 Python 进程仍然占用高达 7 GB 的内存。

知道为什么会这样吗?我正在使用 Pandas 0.20.3。

下面的最小示例。实际上,“数据”变量将有大约 1500 万行,但我不知道如何将其发布在这里。

import datetime, pandas as pd

data = {'Time':['2013-10-29 00:00:00', '2013-10-29 00:00:08', '2013-11-14 00:00:00'], 'Watts': [0, 48, 0]}
df = pd.DataFrame(data, columns = ['Time', 'Watts'])
# Convert string to datetime
df['Time'] = pd.to_datetime(df['Time'])
# Make column Time as the index of the dataframe
df.index = df['Time']
# Delete the column time
df = df.drop('Time', 1)

# Get the difference in time between two consecutive data points
differences = df.index.to_series().diff()
# Keep only the differences > 60 mins
differences = differences[differences > datetime.timedelta(minutes=60)]
# Get the string of the day of the data points when the data gathering resumed
toRemove = [datetime.datetime.strftime(date, '%Y-%m-%d') for date in differences.index.date]

# Remove data points belonging to the day where the differences was > 60 mins
for dataPoint in toRemove:
df.drop(df[dataPoint].index, inplace=True)

最佳答案

您可能想尝试调用垃圾收集器。 gc.collect()How can I explicitly free memory in Python?想要查询更多的信息

关于python - Pandas - 巨大的内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48422642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com