如何快速制作包含日期键的哈希
喜欢
start_date = Date.new(2017, 1, 9)
end_date = Date.new(2017, 1, 12)
如何获得
result =
{
"2017-01-09": "",
"2017-01-10": "",
"2017-01-11": "",
"2017-01-12": "",
}
答案 0 :(得分:3)
DECLARE
Date_Parameter date;
Max_Month date;
BEGIN
Date_Parameter := DATE '2017-04-01';
select max(month) into Max_month from BALANCE_YEAR2;
select
(
IF Date_Parameter <= Max_month THEN
OPEN :to_grid FOR
select sum(FLARE_MTD_KNM3) FLARE_MTD_KNM3
from BALANCE_YEAR2
where month between trunc(Date_Parameter, 'YEAR')
and LAST_DAY(date_parameter);
ELSE
OPEN :to_grid FOR
select NET_VOL_MTD_KNM3
from STREAM_D
where code = 'FLARE'
and production_day = LAST_DAY(Date_Parameter)
END IF
),
(
select sum(max(case when code = 'U900' then volume else null end )) CORR
from BALANCE_YEAR1
where production_day between trunc(Date_Parameter, 'YEAR') and Max_month
group by production_day
) from dual;
END;
/
或
(start_date..end_date).map(&:to_s).product([""]).to_h
@Stefan在评论中正确注意到 NB ,在上面的两个片段中,所有哈希值都引用相同的(start_date..end_date).map(&:to_s).zip([""].cycle).to_h
实例。如果它不是一个选项,那么你手头有另一个(我最喜欢的)解决方案:
String
后一种解决方案有一个额外的好处:
Hash.new { |h, k| h[k.to_s] = ""}.tap do |h|
h.values_at(*(start_date..end_date))
end
现在让我们来处理新的日期:
hash = Hash.new { |h, k| h[k.to_s] = ""}.tap do |h|
h.values_at(*(start_date..end_date))
end
#⇒ {"2017-01-09"=>"", ... "2017-01-12"=>""}
答案 1 :(得分:2)
(start_date..end_date).each_with_object({}) { |date, hash| hash[date.to_s] = '' }
#=> {"2017-01-09"=>"", "2017-01-10"=>"", "2017-01-11"=>"", "2017-01-12"=>""}
答案 2 :(得分:2)
您只需使用
即可 df_ID = pd.DataFrame({'Name': ['CTA15', 'CTA16'],
'AA_ID': [22, 22'],
'BB_ID':[4, 5],
'CC_ID' : [2, 2]})