从一个包含约1500万行数据(占用约250MB)的pickle文件中加载数据框后,我对其进行了一些搜索操作,并删除了其中的一些行。在这些操作期间,内存使用量急剧上升至5GB或7GB左右,这很烦人,因为会发生交换(我的笔记本只有8GB内存)。
问题在于当操作完成时(即执行下面代码中的最后两行时),这些内存并没有被释放。因此,Python进程仍然占据了高达7GB的内存。
不知道为什么会出现这种情况,请问有什么想法吗?我正在使用Pandas 0.20.3。
下面是一个最小化的示例。实际上,“data”变量将有大约1500万行,但我不知道如何在这里发布它。
问题在于当操作完成时(即执行下面代码中的最后两行时),这些内存并没有被释放。因此,Python进程仍然占据了高达7GB的内存。
不知道为什么会出现这种情况,请问有什么想法吗?我正在使用Pandas 0.20.3。
下面是一个最小化的示例。实际上,“data”变量将有大约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)