我正在尝试使用Pandas创建交易日历。我能够基于USFederalHolidayCalendar创建一个cal实例。 USFederalHolidayCalendar与交易日历不一致,因为交易日历不包括哥伦布日和退伍军人日。但是,交易日历包括耶稣受难日(不包括在USFederalHolidayCalendar中)。以下代码中除最后一行之外的所有内容都有效:
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
from datetime import datetime
cal = get_calendar('USFederalHolidayCalendar') # Create calendar instance
cal.rules.pop(7) # Remove Veteran's Day rule
cal.rules.pop(6) # Remove Columbus Day rule
tradingCal = HolidayCalendarFactory('TradingCalendar', cal, GoodFriday)
tradeCal实例似乎有效,我可以查看Holiday规则。
In[10]: tradingCal.rules
Out[10]:
[Holiday: Labor Day (month=9, day=1, offset=<DateOffset: kwds={'weekday': MO(+1)}>),
Holiday: Presidents Day (month=2, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: Good Friday (month=1, day=1, offset=[<Easter>, <-2 * Days>]),
Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Thanksgiving (month=11, day=1, offset=<DateOffset: kwds={'weekday': TH(+4)}>),
Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Christmas (month=12, day=25, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: MemorialDay (month=5, day=31, offset=<DateOffset: kwds={'weekday': MO(-1)}>)]
当我尝试在日期范围内列出假期时,我收到以下错误:
In[11]: tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-12-2708cd2db7a0>", line 1, in <module>
tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
TypeError: unbound method holidays() must be called with TradingCalendar instance as first argument (got datetime instance instead)
有什么想法吗?
答案 0 :(得分:34)
或许从头开始创建交易日历更为直接,如下:
- (IBAction)selectPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
selectedImageView = self.imageView; // Add this
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
selectedImageView = self.imageView; // Add this
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)takePhotoTwo:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
selectedImageView = self.imageTwo; // Add this
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)selectPhotoTwo:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
selectedImageView = self.imageTwo; // Add this
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
selectedImageView.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
答案 1 :(得分:17)
如果有帮助,我对交易所交易日历也有类似需求。 Quantopian在Zipline项目中埋藏了一些优秀的代码。我提取了相关部分并创建了一个用于在熊猫中创建市场交易日历的新项目。链接在这里,具有下面描述的一些功能。
https://github.com/rsheftel/pandas_market_calendars
https://pypi.python.org/pypi/pandas-market-calendars
通过为纽约证券交易所创建所有有效营业时间的pandas DatetimeIndex,它可以做些什么:
import pandas_market_calendars as mcal
nyse = mcal.get_calendar('NYSE')
early = nyse.schedule(start_date='2012-07-01', end_date='2012-07-10')
early
market_open market_close
=========== ========================= =========================
2012-07-02 2012-07-02 13:30:00+00:00 2012-07-02 20:00:00+00:00
2012-07-03 2012-07-03 13:30:00+00:00 2012-07-03 17:00:00+00:00
2012-07-05 2012-07-05 13:30:00+00:00 2012-07-05 20:00:00+00:00
2012-07-06 2012-07-06 13:30:00+00:00 2012-07-06 20:00:00+00:00
2012-07-09 2012-07-09 13:30:00+00:00 2012-07-09 20:00:00+00:00
2012-07-10 2012-07-10 13:30:00+00:00 2012-07-10 20:00:00+00:00
mcal.date_range(early, frequency='1D')
DatetimeIndex(['2012-07-02 20:00:00+00:00', '2012-07-03 17:00:00+00:00',
'2012-07-05 20:00:00+00:00', '2012-07-06 20:00:00+00:00',
'2012-07-09 20:00:00+00:00', '2012-07-10 20:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None)
mcal.date_range(early, frequency='1H')
DatetimeIndex(['2012-07-02 14:30:00+00:00', '2012-07-02 15:30:00+00:00',
'2012-07-02 16:30:00+00:00', '2012-07-02 17:30:00+00:00',
'2012-07-02 18:30:00+00:00', '2012-07-02 19:30:00+00:00',
'2012-07-02 20:00:00+00:00', '2012-07-03 14:30:00+00:00',
'2012-07-03 15:30:00+00:00', '2012-07-03 16:30:00+00:00',
'2012-07-03 17:00:00+00:00', '2012-07-05 14:30:00+00:00',
'2012-07-05 15:30:00+00:00', '2012-07-05 16:30:00+00:00',
'2012-07-05 17:30:00+00:00', '2012-07-05 18:30:00+00:00',
'2012-07-05 19:30:00+00:00', '2012-07-05 20:00:00+00:00',
'2012-07-06 14:30:00+00:00', '2012-07-06 15:30:00+00:00',
'2012-07-06 16:30:00+00:00', '2012-07-06 17:30:00+00:00',
'2012-07-06 18:30:00+00:00', '2012-07-06 19:30:00+00:00',
'2012-07-06 20:00:00+00:00', '2012-07-09 14:30:00+00:00',
'2012-07-09 15:30:00+00:00', '2012-07-09 16:30:00+00:00',
'2012-07-09 17:30:00+00:00', '2012-07-09 18:30:00+00:00',
'2012-07-09 19:30:00+00:00', '2012-07-09 20:00:00+00:00',
'2012-07-10 14:30:00+00:00', '2012-07-10 15:30:00+00:00',
'2012-07-10 16:30:00+00:00', '2012-07-10 17:30:00+00:00',
'2012-07-10 18:30:00+00:00', '2012-07-10 19:30:00+00:00',
'2012-07-10 20:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None)
如果您只想获得可以在其他pandas函数中使用的pandas Holiday Calendar,那么将其作为参数:
holidays = nyse.holidays()
holidays.holidays[-5:]
(numpy.datetime64('2030-05-27'),
numpy.datetime64('2030-07-04'),
numpy.datetime64('2030-09-02'),
numpy.datetime64('2030-11-28'),
numpy.datetime64('2030-12-25'))
答案 2 :(得分:11)
您必须创建类的新实例:cal1 = tradingCal()
。这对我有用。
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
from datetime import datetime
cal = get_calendar('USFederalHolidayCalendar') # Create calendar instance
cal.rules.pop(7) # Remove Veteran's Day rule
cal.rules.pop(6) # Remove Columbus Day rule
tradingCal = HolidayCalendarFactory('TradingCalendar', cal, GoodFriday)
print tradingCal.rules
#new instance of class
cal1 = tradingCal()
print cal1.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
#DatetimeIndex(['2015-01-01', '2015-01-19', '2015-02-16', '2015-04-03',
# '2015-05-25', '2015-07-03', '2015-09-07', '2015-11-26',
# '2015-12-25', '2016-01-01', '2016-01-18', '2016-02-15',
# '2016-03-25', '2016-05-30', '2016-07-04', '2016-09-05',
# '2016-11-24', '2016-12-26'],
# dtype='datetime64[ns]', freq=None, tz=None)
答案 3 :(得分:0)
我使用相同的软件包采取了略有不同的方法:
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
因此,只需获取您的清单,检查交易日期是否合适,然后继续即可。