Serving the Quantitative Finance Community

  • 1
  • 2
  • 3
  • 4
  • 5
  • 23
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

July 23rd, 2014, 7:16 pm

This one is quite handy
Last edited by tags on July 22nd, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

July 27th, 2014, 6:23 pm

what are the (dis)advantages of the command from [module] import * over import [module],eg from abc import * over import abc?
Last edited by tags on July 26th, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
jimmybob
Posts: 0
Joined: May 10th, 2013, 7:24 am

Python tricks

July 28th, 2014, 8:31 am

QuoteOriginally posted by: tagomawhat are the (dis)advantages of the command from [module] import * over import [module],eg from abc import * over import abc?from [module] import * will import all names in [module] to the global namespace. In general this will pollute the global namespace with lot of stuff you don't want (especially if the module exports a lot of names). import [module] means that the names in module will only be accessible by referencing the module itself e.g. module.function1 , where function1 is a name in the module. Since this doesn't pollute the global namespace, you should basically always use this form.
Last edited by jimmybob on July 27th, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

Python tricks

August 13th, 2014, 7:21 am

QuoteOriginally posted by: jimmybobQuoteOriginally posted by: tagomawhat are the (dis)advantages of the command from [module] import * over import [module],eg from abc import * over import abc?from [module] import * will import all names in [module] to the global namespace. In general this will pollute the global namespace with lot of stuff you don't want (especially if the module exports a lot of names). import [module] means that the names in module will only be accessible by referencing the module itself e.g. module.function1 , where function1 is a name in the module. Since this doesn't pollute the global namespace, you should basically always use this form.Unless you are calling module.function1() a lot of times (say in a loop), in which case you might lose performance because of the extra attribute lookup. So in that case a from module import function1 might be better.
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

September 27th, 2014, 1:31 pm

xlwings seems pretty cool to interfacing python and excel.
 
User avatar
bearish
Posts: 5186
Joined: February 3rd, 2011, 2:19 pm

Python tricks

September 27th, 2014, 8:12 pm

QuoteOriginally posted by: tagomaxlwings seems pretty cool to interfacing python and excel.Superficially, that looks pretty nice. Has anybody around here tried it?
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Python tricks

September 28th, 2014, 10:17 am

QuoteOriginally posted by: bearishQuoteOriginally posted by: tagomaxlwings seems pretty cool to interfacing python and excel.Superficially, that looks pretty nice. Has anybody around here tried it?I did a quick google and found this API It that's what it offers, I scratch head to think why should I use it because I can do the same in C#, Excel-DNA and C++ ATL with ease. And 2-way data transport in C++ is easy as well.I am missing the mission statement, compelling reason for using this API! It probably means you call the nice numerical libraries in Python.And what about performance.
Last edited by Cuchulainn on September 27th, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
katastrofa
Posts: 7440
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Python tricks

July 26th, 2015, 8:32 pm

How to save as PostScript pics with transparent fills (alpha < 1) from mathplotlib in Python? Preferably, obtaining a human readable/editable PS file.
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

July 27th, 2015, 7:44 pm

QuoteOriginally posted by: katastrofaHow to save as PostScript pics with transparent fills (alpha < 1) from mathplotlib in Python? Preferably, obtaining a human readable/editable PS file.are you after the best turnaround as eps doesn't natively support transparency? is it your question?
 
User avatar
katastrofa
Posts: 7440
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Python tricks

July 27th, 2015, 9:09 pm

Apparently eps10 supports it and also some programs simply rasterize the part of the picture with the transparency (as it's done in Matlab in older eps versions).
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

January 24th, 2016, 3:32 pm

Say I have the following data in a pandas.DataFrame object.What is a pythonic way to slice this dataframe to the first index where there is a data available in every column? (ie 24/01/2015)(Notice I don't want to get rid of rows where there is at leat 1 NA in nearest dates.)It is probably basic, but I couldn't come up with an elegant solution to be honest.Date, m1, m2, m3, m420/01/15, NA, NA, 400, NA21/01/15, NA, NA, 401, NA22/01/15, NA, 202, 402, NA23/01/15, NA, 203, 403, 10324/01/15, 100, 204, 404, 10425/01/15, 101, 205, 405, 10526/01/15, 102, 206, 406, 10627/01/15, 103, 207, 407, 10728/01/15, 104, 208, 408, 10829/01/15, NA, NA, NA, NA30/01/15, 106, 210, NA, 11031/01/15, 107, 211, 411, 11101/02/15, 108, 212, 412, 11202/02/15, 109, 213, 413, 11303/02/15, 110, 214, 414, 11404/02/15, 111, 215, 415, 115
Last edited by tags on January 23rd, 2016, 11:00 pm, edited 1 time in total.
 
User avatar
tags
Topic Author
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

Python tricks

February 1st, 2016, 9:45 pm

Say I have a time series containing daily returns over 1 year. I want to calculate the number of times returns were positive for 1 day, the number of times returns were positive for 2 straight days, ... etc .... and the same for negative returns. What is an elegant way to do that? I tried different things using combining diff(), sum(), cumsum(), groupby() (etc...) but wasn't really successful. Your suggestions are welcome! Thanks.
Last edited by tags on January 31st, 2016, 11:00 pm, edited 1 time in total.
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Python tricks

February 1st, 2016, 11:54 pm

Here's some pseudocode for a brute force procedural approach:1. Check the sign of the first day. If it is negative put -1 in a day counter else put +1 in the counter2. Loop through the remaining days3a. If the sign of the day is positive and the sign of the counter is positive, then increment the counter, else push the counter value on a stack and set the counter to -13b. If the sign of the day is nonpositive and the sign of the counter is nonpositive, then decrement the counter, else push the counter value on a stack and set the counter to +14,. loop to the end of days (whether you push the last counter value after the loop exits on the stack depends on how you want to define things*)The resulting stack contains the set of runs, + values for positive runs and - values for negative ones*This is not a trivial decision as is the issue of the first value on the stack. Those first and last values will be biased estimates of run length because they exclude the runs that were ongoing before the window and after the window.