Page 1 of 16

Python tricks

Posted: July 23rd, 2014, 7:16 pm
by tagoma
This one is quite handy

Python tricks

Posted: July 27th, 2014, 6:23 pm
by tagoma
what are the (dis)advantages of the command from [module] import * over import [module],eg from abc import * over import abc?

Python tricks

Posted: July 28th, 2014, 8:31 am
by jimmybob
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.

Python tricks

Posted: August 13th, 2014, 7:21 am
by DevonFangs
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.

Python tricks

Posted: September 27th, 2014, 1:31 pm
by tagoma
xlwings seems pretty cool to interfacing python and excel.

Python tricks

Posted: September 27th, 2014, 8:12 pm
by bearish
QuoteOriginally posted by: tagomaxlwings seems pretty cool to interfacing python and excel.Superficially, that looks pretty nice. Has anybody around here tried it?

Python tricks

Posted: September 28th, 2014, 10:17 am
by Cuchulainn
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.

Python tricks

Posted: July 26th, 2015, 8:32 pm
by katastrofa
How to save as PostScript pics with transparent fills (alpha < 1) from mathplotlib in Python? Preferably, obtaining a human readable/editable PS file.

Python tricks

Posted: July 27th, 2015, 7:44 pm
by tagoma
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?

Python tricks

Posted: July 27th, 2015, 9:09 pm
by katastrofa
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).

Python tricks

Posted: January 24th, 2016, 3:32 pm
by tagoma
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

Python tricks

Posted: February 1st, 2016, 9:45 pm
by tagoma
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.

Python tricks

Posted: February 1st, 2016, 11:54 pm
by Traden4Alpha
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.