I have seen many ways to determine week of the year. Like by giving instruction datetime.date(2016, 2, 14).isocalendar()[1] I get 6 as output. Which means 14th feb 2016 falls under 6th Week of the year. But I couldn’t find any way by which I could find week of the Month. Means IF I give input as some_fu…
Tag: python
Python fast DataFrame concatenation
I wrote a code to concatenate parts of a DataFrame to the same DataFrame as to normalize the occurrence of rows as per a certain column. and this is unbelievably slow. Is there a way to fast concatenate DataFrame without creating copies of it? Answer There are a couple of things that stand out. To begin with,…
Django migrate ProgrammingError: syntax error at or near “”
I’m learning django (1.9.2) on a practice web site and a postgresql database with psycopg2. I defined a model with a certain attribute “preview” and later deleted the attribute entirely. Despite having removed it, django seems to be referencing that old definition perhaps from a cache or som…
Calculating cumulative returns with pandas dataframe
I have this dataframe I’m trying to make a running total of daily_rets in perc_ret however my code just copies the values from daily_rets Answer If performance is important, use numpy.cumprod: Timings:
Group by and find top n value_counts pandas
I have a dataframe of taxi data with two columns that looks like this: Basically, each row represents a taxi pickup in that neighborhood in that borough. Now, I want to find the top 5 neighborhoods in each borough with the most number of pickups. I tried this: Which gives me something like this: How do I filt…
Second newest file
I need the second newest file. In this thread the newest is found: Python get most recent file in a directory with certain extension which uses this construct: However, how can I get not the min or max but the second element? Answer I think this can be a suitable solution: So basically glob.iglob(‘*.log…
Jinja loop.index does not print
When running the following jinja code, I only get “Column info” printed. Why the index does not appears ? Answer It sounds like the template is being treated as a Django template, not a Jinja template. Using {{ loop.index }} should work in a Jinja template, but wouldn’t work in a Django temp…
AWS error from Python: No module named lambda_function
I am creating a AWS Lambda python deployment package. I am using one external dependency requests. I installed the external dependency using the AWS documentation. Below is my Python code. Created the Zip the content of the project-dir directory and uploaded to the lambda(Zip the directory content, not the di…
pexpect.expect() in python3 is throwing error as “must be in str , not bytes”
I am migrating my code to python 3.4.3. This code works fine in python 2.4.3. but here it throws error in python 3.4.3. should I use anything different from expect ? Here is my code snippet which gets the error: The error what I get is : Answer pexpect wants to log bytes, not decoded strings. You can just let
Python Logging – Disable logging from imported modules
I’m using the Python logging module, and would like to disable log messages printed by the third party modules that I import. For example, I’m using something like the following: This prints out my debug messages when I do a logger.debug(“my message!”), but it also prints out the debug…