将日期范围分为多个范围

时间:2019-11-01 19:21:53

标签: python python-3.x csv datetime

我在CSV中有这样的数据:

1940-10-01,somevalue
1940-11-02,somevalue
1940-11-03,somevalue
1940-11-04,somevalue
1940-12-05,somevalue
1940-12-06,somevalue
1941-01-07,somevalue
1941-02-08,somevalue
1941-03-09,somevalue
1941-05-01,somevalue
1941-06-02,somevalue
1941-07-03,somevalue
1941-10-04,somevalue
1941-12-05,somevalue
1941-12-06,somevalue
1942-01-07,somevalue
1942-02-08,somevalue
1942-03-09,somevalue

我想为所有数据将日期从1-oct-year31-march-next-year分开。因此,对于以上输出的数据将是:

1940/1941:

1940-11-02,somevalue
1940-11-03,somevalue
1940-11-04,somevalue
1940-12-05,somevalue
1940-12-06,somevalue
1941-01-07,somevalue
1941-02-08,somevalue
1941-03-09,somevalue

1941/1942:

1941-10-04,somevalue
1941-12-05,somevalue
1941-12-06,somevalue
1942-01-07,somevalue
1942-02-08,somevalue
1942-03-09,somevalue
1942-10-01,somevalue

我的代码踪迹是:

import csv
from datetime import datetime

with open('data.csv','r') as f:
    data = list(csv.reader(f))

quaters = []
year =  datetime.strptime(data[0][0], '%Y-%m-%d').year
for each in data:
    date =  datetime.strptime(each[0], '%Y-%m-%d')
    print(each)        

    if (date>=datetime(year=date.year,month=10,day=1) and date<=datetime(year=date.year+1,month=3,day=31)):
        middle_quaters[-1].append(each)
    if year != date.year:            
        quaters.append([])

但是我没有得到预期的输出。我想将每个日期范围存储在单独的列表中。

3 个答案:

答案 0 :(得分:0)

我会使用pandas dataframe来做到这一点。 会更容易.. 跟随这个 Pandas: Selecting DataFrame rows between two dates (Datetime Index)

对于您的情况

data = pd.read_csv("data.csv")
df.loc[startDate : endDate]



# you can walk through a bunch of ranges like so..
listOfDateRanges = [(), (), ()]
for date_range in listOfDateRanges:
   df.loc[date_range[0] : date_range[1]]

答案 1 :(得分:0)

为此,您可以使用pandas库。这是相同的示例代码:

import pandas as pd
df = pd.read_csv('so.csv', parse_dates=['timestamp'])   #timestamp is your time column
current_year, next_year = 1940, 1941
df = df.query(f'(timestamp >= "{current_year}-10-01") & (timestamp <= "{next_year}-03-31")')
print (df)

这将为您的数据提供以下结果:

   timestamp      value
0 1940-10-01  somevalue
1 1940-11-02  somevalue
2 1940-11-03  somevalue
3 1940-11-04  somevalue
4 1940-12-05  somevalue
5 1940-12-06  somevalue
6 1941-01-07  somevalue
7 1941-02-08  somevalue
8 1941-03-09  somevalue

希望这会有所帮助!

答案 2 :(得分:0)

没有外部程序包...根据选择的字段创建一个查找,然后对其进行整型,并进行小于等于大于确定范围。

import re

data = '''1940-10-01,somevalue
1940-11-02,somevalue
1940-11-03,somevalue
1940-11-04,somevalue
1940-12-05,somevalue
1940-12-06,somevalue
1941-01-07,somevalue
1941-02-08,somevalue
1941-03-09,somevalue
1941-05-01,somevalue
1941-06-02,somevalue
1941-07-03,somevalue
1941-10-04,somevalue
1941-12-05,somevalue
1941-12-06,somevalue
1942-01-07,somevalue
1942-02-08,somevalue
1942-03-09,somevalue'''

lookup={}
lines = data.split('\n')
for line in lines:
    d = re.sub(r'-','',line.split(',')[0])
    lookup[d]=line

dates=sorted(lookup.keys())

_in=19401201
out=19411004
outfile=[]
for date in dates:
    if int(date) > _in and int(date) < out:
        outfile.append(lookup[date])

for l in outfile:
    print outfile