【发布时间】:2014-12-09 13:37:53
【问题描述】:
我有一个带有列的时间序列表(在 Postgres 数据库中)
item_id, country_id, year, month, value
在此表中有重复的时间序列:它们具有相同的 country_id 和时间序列日期/值,但分配了不同的 item_id,例如:'Red Apples' 和 'Apples, Red'
如何识别这些重复的时间序列?我希望 (country_id, year, month, value) 匹配该项目存在的所有日期。
我是初学者,所以请原谅我遗漏的任何细节。我主要在寻找概念方法 - 我可以在 Postgres 或 python/Pandas 中实现它。
例如,我希望能够检测到这样的事情:
item_id, country_id, year, month, value
-------------------------------------------------------
Red Apples 5 1996 1 300
Red Apples 5 1996 2 500
Red Apples 5 1996 3 370
Apples, Red 5 1996 1 300
Apples, Red 5 1996 2 500
Apples, Red 5 1996 3 370
我希望输出如下所示:
item_id1, item_id2, country_id, year, month_range
-----------------------------------------------------------------
Red Apples Apples, Red 5 1996 [1,3]
这样也行:
item_id1, item_id2, country_id, year, time_month, value
--------------------------------------------------------------------------
Red Apples Apples, Red 5 1996 1 300
Red Apples Apples, Red 5 1996 2 500
Red Apples Apples, Red 5 1996 3 370
我想过尝试这样的事情:
select distinct A.country_id, A.item_id, B.item_id, A.year, A.month, A.value
from my_table as A,
my_table as B
where
(A.country_id=B.country_id and
A.item_id<>B.item_id and
A.year=B.year and
A.month=B.month and
A.value=B.value )
然后我会检查以确保所有日期/值都出现在每个已识别的 item_id 对中。 但如果可能的话,我想一次检查所有日期/值。
我不确定表连接是否合适...?
【问题讨论】:
-
如果你的数据有另一个条目,比如
Yellow Bananas,5,1996,1,300——这里也算重复吗? -
我只想识别重复的时间序列或至少子序列。并非只有一个日期的巧合。
-
系列的最小长度是多少?以及如何处理跨年份的系列,如
1996-12,1997-1? -
每个 (item_id, country_id) 对会有几年的数据,我想找到至少连续 3 个月是相同的。输出的具体格式并不重要,只要因为它返回值相同的所有 item_ids 和 country_ids & 日期。
标签: python sql postgresql time-series