text
stringlengths
46
37.3k
title
stringlengths
12
162
Python : What the normal style for data objects in python . Lets say I have a method that gets a customer from somewhere ( net , DB , ... . ) what type of object should I return . I see several choices : a tuplea dictionarya class instance ( are data classes 'normal ' ) I am sure there are others . Doing my first big p...
what is the accepted style python for data objects
Python : I have been trying to delete multiple dictionaries in a list but I can only delete one at a time . Below is the main code I am working on . Records is the list of dictionaries . I want to delete dictionaries that have 0 in them.I want to delete dictionaries with Prices of 0 <code> Records = [ { 'Name ' : 'Kelv...
Delete multiple dictionaries in a list
Python : Basically I want to go from -1 to 1 in n steps , including -1 and 1 : How can I write this in the most elegant , simplest way for any n value ? <code> x = -1.0n = 21for i in range ( n ) : print x x += 0.01-1.0 -0.9 -0.8 ... 0.8 0.9 1.0
What 's the most elegant way to write this for loop in Python ?
Python : I have some financial data and want to get only the last transaction from a specific period of time ( hours , days , months ... ) .Example : I was thinking in using groupby ( ) and get the mean ( ) for that day ( this solution is also possible for my problem , but not exactly ) but do n't know how to select th...
Pandas/Python - Group data by same period in time
Python : I 'm a Python developer , making my first steps in JavaScript.I started using Map and Set . They seem to have the same API as dict and set in Python , so I assumed they 're a hashtable and I can count on O ( 1 ) lookup time.But then , out of curiosity , I tried to see what would happen if I were to do this in ...
Does JavaScript use hashtables for Map and Set ?
Python : When I extended some tool generated classes , I did n't realize that they are old style classes until I tried to use super ( ) . The super ( ) does n't work with old style classes , so I got this error : E.g. , try this snippet : I was just curious what would happen if I extended B from object as well to make ...
Is it OK to extend both old and new style classes ?
Python : I am trying to parse to an xml into multiple different Files -Sample XMLThe goal is to have common xml to csv conversion to be put in place . Based on input file the xml should be flattend and exploded into multiple csv and stored.The input is an xml which is above and config csv file below . Need to create 3 ...
Create CSV from XML/Json using Python Pandas
Python : I want to access the serial port using Crystal lang.I have following code in python . I want to write the equivalent Crystal-lang code for a pet project.I could n't find any library for accessing the serial port.What is the proper way for accessing serial port in crystal-lang ? Thanks in advance . <code> impor...
Crystal-lang Accessing Serial port
Python : With input = [ 0,0,5,9,0,4,10,3,0 ] as listI need an output , which is going to be two highest values in input while setting other list elements to zero . output = [ 0,0,0,9,0,0,10,0,0 ] The closest I got : Can you please help ? <code> from itertools import compressimport numpy as npimport operatorinput= [ 0,0...
How to filter Python list while keeping filtered values zero
Python : I have a multidimensional numpy array where the elements are either True or False values : Now I need to generate another array M , where the value at each site M [ i , j ] depends on grid [ i : i+2 , j : j+2 ] as inIs there some way to fill the array M using elements from grid without double loops ? <code> im...
Check if all elements are True in sliding windows across a 2D array - Python
Python : I have two DataFrames ( with DatetimeIndex ) and want to update the first frame ( the older one ) with data from the second frame ( the newer one ) . The new frame may contain more recent data for rows already contained in the the old frame . In this case , data in the old frame should be overwritten with data...
Setting DataFrame values with enlargement
Python : Im working on a detecting alogrythm for detecting storm cells on a radar imagery.I have radar data in 2d numpy arrays that we plot on a basemap.We got azymuth and rangebins data that we put in a polargrid with lat/lon coordinates.The values in our numpy array are based on dBZ height in range from zero till max...
detect high values from numpy array
Python : What follows is four functions that have the same output , but either written with a list comprehension or a tight loop , and a function call to vs an inline condition.Interestingly , a and b have the same bytecode when disassembled , however b is much faster than a.Moreover , d , which uses a tight loop with ...
Why do these two functions have the same bytecode when disassembled under dis.dis ?
Python : I have a parser function which returns iter ( iter ( tree ) ) . How can I convert the parsedSentence type to list ( tree ) and access 1st element of that list . I 've already tried list ( parser.raw_parse_sents ( [ sentence ] , False ) ) but it 's not converting the result to list . Edited : Here it throws an ...
Python - NLP - convert iter ( iter ( tree ) ) to list ( tree )
Python : I have a dictionary in the following formand I need the reverse index dictionary , in this form : BasicallySo essentially , I need the keys of the values as keys , and the keys as keys of values , while at the same time joining results for duplicate new keys etc.I 've tried to do if this makes any senseBut I '...
In python , how do I invert a 2D dictionary ?
Python : I 'm setting up virtual env . I was getting warnings about an outdated pip ( 19.2 ) so I updated pip on my ( macos ) system globally , sudo -H python3 -m pip install -- upgrade pip . It seems to have worked , but when I make a new venv , I 'm still getting the old pip version.Where is the older version coming ...
Why is my venv using a different pip version than I have installed
Python : I 'm a minor contributor to a package where people are meant to do this ( Foo.Bar.Bar is a class ) : Sometimes people do this by mistake ( Foo.Bar is a module ) : This might seems simple , but users still fail to debug it , I would like to make it easier . I ca n't change the names of Foo or Bar but I would li...
Altering traceback of a non-callable module
Python : In the official python documentation in the Data model section , the __ipow__ method is defined as : Then , the documentation explains that These methods are called to implement the augmented arithmetic assignments ( **= for __ipow__ ) But what is the syntax of **= that allows to use the modulo argument ? <cod...
python ipow : how to use the third argument ?
Python : I am trying to implement expandable CNN by using maclaurin series . The basic idea is the first input node can be decomposed into multiple nodes with different orders and coefficients . Decomposing single nodes to multiple ones can generate different non-linear line connection that generated by maclaurin serie...
How to implement maclaurin series in keras ?
Python : Perl has a construct ( called a `` hash slice '' as Joe Z points out ) for indexing into a hash with a list to get a list , for example : executed gives : the simplest , most readable way I know to approach this in Python would be a list comprehension : because : is there some other , better way I 'm missing ?...
index into Python dict with list to get a list , as with a Perl hash
Python : I have the following directory structure : I wish to import package.so within file1.py.I 've tried the following import statements to no avail : I want to avoid having to set PYTHONPATH / sys.path.Is there a simple way to do this ? I assume the issue is due to the package being a shared object and not just a P...
Import binary package from different directory
Python : I am aware of the existence and purpose of collections.namedtuple , but I have noticed that , at least in IDLE ( 3.2.2 ) , this factory function is also in functools : It also exists in collections as expected , and is the same function : No docs I can find ever mention namedtuple being anywhere other than col...
Python functools.namedtuple
Python : I 'm trying to develop a python script for blender to output a rendered image sequence to a PDF . I am using Imagemagick to convert to PDF , that part is working fine , However , I want the thumbnail preview to also be included in the PDF.The PDF format is a bit confusing to me , but I have found the /PageMode...
Editing PDF attributes using sed
Python : Given a textfile of lines of 3-tuples : The goal is to achieve two different data types : sents_with_positions : a list of list of tuples where the the tuples looks like each line of the textfilesents_words : a list of list of string made up of only the third element in the tuples from each line of the textfil...
Unpacking tuple-like textfile
Python : BackgroundI 'm trying to figure out Python 's descriptors by reading Lutz 's Learning Python 's section on the topic in which he says : `` Like properties , descriptors are designed to handle specific attributes ... Unlike properties , descriptors have their own state ... '' Throughout the chapter he shows exa...
Where does a python descriptor 's state go ?
Python : I am experimenting with writing a library in Rust that I can call from Python code . I would like to be able to pass a void pointer back to Python so that I can hold state between calls into Rust . However , I get a segfault in Rust when trying to access the pointer again.Full code samples and crash report : h...
How can a pointer be passed between Rust and Python ?
Python : I am using Cython as part of my build setup for a large project , driven by CMake.I ca n't seem to get Cython to generate the .c files in a sensible location.My file layout : My setup.py is generated by CMake ( a lot of it depends on configure_file ) , in the location specified above.This location conforms to ...
Can I achieve precise control over location of .c files generated by cythonize ?
Python : Most array-oriented languages such as APL or J have some form of generalized inner product , which can act like standard matrix multiplication , but supports arbitrary operations in place of the standard ones . For example , in J +/ . * is the standard multiply-then-sum , but you can also do e.g . < ./ . + to ...
Does numpy provide a generalized inner product ?
Python : If I set up a class like below in Python , as I expect the lambda expressions created should be bound to the class A. I do n't understand why when I put a lambda inside a list like in g it is n't bound.Why is one bound and not the other ? <code> class A ( object ) : f = lambda x , y : ( x + y ) g = [ lambda x ...
How are lambda expressions bound to a class ?
Python : I want to use http auth but also , a reverse proxy using gunicorn.For http auth I use : for gunicorn , proxy reverse I found : How can I combine both ? <code> location = admin.html { auth_basic 'Login Required ' auth_basic__use_file etc/nginx/.htpasswd ; } try_files $ uri @ gunicorn ;
Nginx with gunicorn with double authorization
Python : I have the following in my app : I only want a university to be related to one other university in both directions of that relationship.For example , in the database , if I select university A as the sister_university of university B , I only want to be allowed to select university B as the sister_university u...
I have a OneToOne relationship between two objects of the same class in a Django app . Is it possible to enforce the uniqueness of this relationship ?
Python : I have an async websockets listener . That listener passes on messages from a sync main-loop.I would like to let the async websockets listener know that there 's a new message to be send.Currently I implemented that using a polling-loop ( bad ) . I tried using cond.notify_all ( ) but that can not be used outsi...
How can I notify an async routine from a sync routine ?
Python : Consider the sorted array a : If I specified left and right deltas , Then this is how I 'd expect the clusters to be assigned : NOTE : Despite the interval [ -1 , 1 ] sharing an edge with [ 1 , 3 ] , neither interval includes an adjacent point and therefore do not constitute joining their respective clusters.A...
Identify clusters linked by delta to the left and different delta to the right
Python : I am working on Python 2.6/2.7 code which contains the following : I can understand the try-except part , which is used to see if gmpy has been installed on the system -- and if not , to do whatever . However , I do not understand why the if gmpy.__file__ is None check is necessary ; it seems redundant.Are the...
Python : can __file__ be None if import has succeeded ?
Python : I have a ( potentially quite big ) dictionary and a list of 'possible ' keys . I want to quickly find which of the keys have matching values in the dictionary . I 've found lots of discussion of single dictionary values here and here , but no discussion of speed or multiple entries.I 've come up with four ways...
Finding all keys in a dictionary from a given list QUICKLY
Python : I have a module that I import to my main application called pageprocs.py with a collection of functions in it that generate different content and return it in a string . pageprocs is supposed to be a way of allowing authenticated users to create plugins for the different content type.I then have a list of stri...
Process functions from a list of strings in Python
Python : I 'd like to represent a set of integer ranges using Python where the set could be modified dynamically and tested for inclusion . Specifically I want to apply this to address ranges or line numbers in a file . I could define the range of addresses I cared about to include : Then I want to be able to add a pot...
Python representation for a set of non-overlapping integer ranges
Python : When i use client1 = HTTPClient ( '192.168.1.2 ' , ' 3 ' ) only it works but when i use both as below : client1 = HTTPClient ( '192.168.1.2 ' , ' 3 ' ) client2 = HTTPClient ( '192.168.1.3 ' , ' 3 ' ) then the whole thing become like very slow and sometimes one of them fails . How to make sure that client1 and ...
Python - how can i make the client to be able to connect multiple times ?
Python : I want to be able to create some turtles which display values by subclassing turtle.Turtle.These turtles should display their value as text centered in their own shape . I also want to be able to position the turtles with accuracy , so setting/determining their width and height relative to a given font size is...
Python Turtle Write Value in Containing Box
Python : This is my project structure : File b.py is empty.File __init__.py is one line : Then the following program gives inconsistent result of a.b : What is the best way of detecting this kind of name conflict between a variable and a filename ? <code> a├── b.py└── __init__.py b = 'this is a str ' import aprint ( a....
Python variable and filename conflicts
Python : Instead of using common OOP , like Java and C # do with their base class Object or object , Python uses special methods for basic behaviour of objects . Python uses __str__ which is used when the object is passed to print : The same with len : What I would expect is something like this : What is the reason for...
Reason for uncommon OOP in Python ?
Python : Say I want to implement a metaclass that should serve as a class factory . But unlike the type constructor , which takes 3 arguments , my metaclass should be callable without any arguments : For this purpose I defined a custom __new__ method with no parameters : But the problem is that python automatically cal...
What 's the correct way to implement a metaclass with a different signature than ` type ` ?
Python : Can someone explain why the Python interpreter ( 2.7.3 ) gives the following : Is this ever useful , and for what purpose ? <code> > > > 5 -+-+-+ 23
Multiple operators between operands
Python : Containers that take hashable objects ( such as dict keys or set items ) . As such , a dictionary can only have one key with the value 1 , 1.0 or True etc . ( note : simplified somewhat - hash collisions are permitted , but these values are considered equal ) My question is : is the parsing order well-defined ...
Dict/Set Parsing Order Consistency
Python : I 'm trying to remove the duplicates out of tuples in a list and add them in a new list with out duplicates , I tried to make two loops but and check for duplicates or sets but the problem there 's three tuplescan anyone help me , I 'm stuck hereexample Output <code> [ ( 2 , 5 ) , ( 3 , 5 ) , ( 2 , 5 ) ] [ 2 ,...
How to flatten a list of tuples and remove the duplicates ?
Python : Let 's say I have a list of filesand I need to sort them into sub-lists based off of their number so thatI could write a bunch of loops , however I am wondering if there is a better way to do this ? <code> files = [ 's1.txt ' , 'ai1.txt ' , 's2.txt ' , 'ai3.txt ' ] files = [ [ 's1.txt ' , 'ai1.txt ' ] , [ 's2....
Sorting files in a list
Python : I have a numpy array that is split by each row : I was hoping to merge said splitArray every 4 rows , and the last subarray not necessarily having to be 4 , but just the remainder of what 's left . Below is the array I hope to have : <code> splitArray : [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0...
python/numpy combine subarrays 4 rows at a time
Python : I 've never seen an import issue like this before . I removed a directory from site-packages and the corresponding package is still importable.However this directory does n't actually existI 've removed everything that I 'm aware of that is related to it , but there must still be something hanging around.Diggi...
importing a package that does n't exist
Python : Given a list of input ( let 's say they are just integers ) , and a list of functions ( and these functions takes an integer , and returns either True or False ) .I have to take this list of input , and see if any function in the list would return True for any value in the list.Is there any way to do this fast...
Search algorithm but for functions
Python : I am new member here and I 'm gon na drive straight into this as I 've spent my whole Sunday trying to get my head around it.I 'm new to Python , having previously learned coding on C++ to a basic-intermediate level ( it was a 10-week university module ) .I 'm trying a couple of iterative techniques to calcula...
Ca n't accurately calculate pi on Python
Python : I have two numpy arrays `` Elements '' and `` nodes '' . My aim is to gather some data of these arrays.I need to replace `` Elements '' data of the two last columns by the two coordinates containsin `` nodes '' array . The two arrays are very huge , I have to automate it.This posts refers to an old one : Repla...
Replace data of an array by two values of a second array
Python : I 'm looking for a way to create hierarchy in form of child parent relationship between two or more instances of same class.How would one go about creating such objects from nested dictionary like in example ? Is this even possible ? Is there some other way which would be recommended to do such task ? I unders...
Constructing hierarchy from dictionary/JSON
Python : I am trying to fit Blaze data object to scikit kmeans function.Data Sample : Its throwing error : I have been able to do it with Pandas Dataframe . Any way to feed blaze object to this function ? <code> from blaze import *from sklearn.cluster import KMeansdata_numeric = Data ( 'data.csv ' ) data_cluster = KMea...
Blaze with Scikit Learn K-Means
Python : I have what is essentially the following in python : What I would like to do is add a dynamic property to all instances of X allowing acces to Y that implicitly fills in the first parameter of the method with the given instance . E.G I would like the following code to work identically : There are several metho...
Partially Evaluating Python Classmethod Based on Where It 's Accessed From
Python : I would like to split a string by ' : ' and ' ' characters . However , i would like to ignore two spaces ' ' and two colons ' : : ' . for e.g.should split intoafter following the Regular Expressions HOWTO on the python website , i managed to comeup with the following However this does not work as intended as i...
Python split a string using regex
Python : I found that a bottleneck in my program is the creation of numpy arrays from a list of given values , most commonly putting four values into a 2x2 array . There is an obvious , easy-to-read way to do it : which takes 15 us -- very very slow since I 'm doing it millions of times.Then there is a far faster , har...
Building a small numpy array from individual values : Fast and readable method ?
Python : Coming from much less dynamic C++ , I have some trouble understanding the behaviour of this Python ( 2.7 ) code.Note : I am aware that this is bad programming style / evil , but I would like to understand it non the less.This code runs without error , and f manipulates the ( seemingly ) global list . This is c...
Writing ( and not ) to global variable in Python
Python : I have a data set where each samples has a structure similar to thisfor example : so for every sample I need to calculate the dot product between every element of x with corresponding element of y of same index and sum the result . i.e : This is my whole code : as you can see the complexity of this algorithm i...
efficient algorithm instead of looping
Python : Every time that I run python or python3 with an interactive console , the display of the prompt gets out of sync almost immediately after the first or second interaction : Then , when I exit out of python , this behavior carries over to bash , with the addition that when you type , nothing appears on the scree...
Terminal display of input goes out of sync while/after using python ? ( temporary fix = ` reset ` )
Python : I 've written a scraper in python using BeautifulSoup library to parse all the names traversing different pages of a website . I could manage it if it were not for more than one urls with different pagination , meaning some urls have pagination some does not as the content are few . My question is : how could ...
Unable to exhaust the content of all the identical urls used within my scraper
Python : I am having trouble figuring out how to arrange my axes so I can perform the following operations in a vectorized way.Essentially I have an array of vectors , an array of matrices , and I want to evaluate VMV^T for each corresponding vector V and matrix MIf it 's simpler , vectorizing the intermediate result b...
Vectorizing multiple vector-matrix multiplications in NumPy
Python : How can I keep the try block as small as possible when I have to catch an exception which can occur in a generator ? A typical situation looks like this : If g ( ) can raise an exception I need to catch , the first approach is this : But this will also catch the SomeException if it occurs in process ( i ) ( th...
Keeping try block small when catching exceptions in generator
Python : I am subclassing a keras model with custom layers . Each layer wraps a dictionary of parameters that is used when generating they layers . It seems these param dictionaries are not set before the training checkpoint is made in Tensorflow , they are set after , which causes an error . I am not sure how to fix t...
Tensorflow Checkpoint Custom Map
Python : I am making a pelican plugin and I 'm having trouble adding variables to the templates.For example in my plugin 's code : And in my templates I have : And I would expect lorem - bar to show in the < h1 > .I have been looking at https : //github.com/getpelican/pelican/blob/807b3bced38bff7b83a2efa2ce8cda9d644eba...
Pelican Plugin - How to add context variables ?
Python : I 'm new to python and programming in general , so would really appreciate any clarification on this point.For example , in the following code : The pure function version seems cleaner and gives the same result.Is the primary value of classes that you can create and call a number of methods on an object , e.g ...
When is it best to use a class in Python ?
Python : Why does the following example fail to run its doctest in the setter method ? The debugger confirms that no test is run ( example above written to dtest.py ) : The same test in the getter method is correctly executed , reporting failure of course ... <code> class Foo : a = None @ property def a ( self ) : pass...
python-2.7 : doctests ignored in setter method of a class
Python : I was trying to figure out how python store the reference count of an object : In my above snippet , as soon as i create a string object s i got the ref-count 28 , then when i assigned inside a dictionary its ref-count increment by one . And I do n't know why it starts with 28.So , Here I am trying to figure o...
How python object store the reference counter for garbage collection
Python : This is driving me totally nuts , I 've been struggling with it for many hours . Any help would be much appreciated . I 'm using PyQuery 1.2.9 ( which is built on top of lxml ) to scrape this URL . I just want to get a list of all the links in the .linkoutlist section . This is my request in full : But that re...
Using lxml to parse namepaced HTML ?
Python : I have trained a CNN in Matlab 2019b that classifies images between three classes . When this CNN was tested in Matlab it was functioning fine and only took 10-15 seconds to classify an image . I used the exportONNXNetwork function in Maltab so that I can implement my CNN in Tensorflow . This is the code I am ...
Why CNN running in python is extremely slow in comparison to Matlab ?
Python : I am currently working on developing an algorithm to determine centroid positions from ( Brightfield ) microscopy images of bacterial clusters . This is currently a major open problem in image processing.This question is a follow-up to : Python/OpenCV — Matching Centroid Points of Bacteria in Two Images.Curren...
Python/OpenCV — Centroid Determination in Bacterial Clusters
Python : I have a pandas dataframe which looks like that : I would like to remove rows based on other rows values with these criterias : A row ( r1 ) must be removed if another row ( r2 ) exist with the same sseqid and r1 [ qstart ] > r2 [ qstart ] and r1 [ qend ] < r2 [ qend ] .Is this possible with pandas ? <code> qs...
Pandas : Delete rows based on other rows
Python : I am writing a time consuming program . To reduce the time , I have tried my best to use numpy.dot instead of for loops.However , I found vectorized program to have much worse performance than the for loop version : What is happening here ? <code> import numpy as npimport datetimekpt_list = np.zeros ( ( 10000,...
performance loss after vectorization in numpy
Python : I need to calculate some probabilities given certain conditions , so I 'm using a function to get the rows that contain givens values , for example : This is the functionSo if I do : I need to generalize the function , so that I can give it more than one parameter and do the following : or generalizedI think a...
Generalize a function in python
Python : Recently I encountered this problem : Say there is a list of something I want to process : And I want to exclude something using another list , for instance : The process_list should be like this after I apply the exclude_list to it ( any process_list item that contains a sub : or if the exclude_list is : excl...
Elegant way to delete items in a list which do not has substrings that appear in another list
Python : Given a string of characters , I want to create a dictionary of all the n-character substrings contained in the string , where the dictionary key is the substring and the value is a list . The first element of the list is the number of occurrences of the substring , and the second element of the list is a list...
Can python dictionary comprehension be used to create a dictionary of substrings and their locations ?
Python : I 'm working on a simple bioinformatics problem . I have a working solution , but it is absurdly inefficient . How can I increase my efficiency ? Problem : Find patterns of length k in the string g , given that the k-mer can have up to d mismatches . And these strings and patterns are all genomic -- so our set...
Efficiency of finding mismatched patterns
Python : Some of my remote Celery tasks never seem to make it to my broker ( RabbitMQ ) . This appears to happen randomly . There are NO errors in my logs and they never make it to the workers or fail . Flower/Rabbit never reports a task failure . I used tcpflow -p -c -i eth0 port 5672 to monitor traffic on the API sen...
Spot the Difference , Celery Task Fails Randomly With No Errors
Python : In this code , why not just define the functions makeitalic ( ) and makebold ( ) and pass in the function hello ? Am I missing something here or are decorators really better for more complicated things ? <code> def makebold ( fn ) : def wrapped ( ) : return `` < b > '' + fn ( ) + `` < /b > '' return wrappeddef...
Python decorators vs passing functions
Python : Based on that answer here are two versions of merge function used for mergesort.Could you help me to understand why the second one is much faster.I have tested it for list of 50000 and the second one is 8 times faster ( Gist ) ..Here is the sort function : . <code> def merge1 ( left , right ) : i = j = inv = 0...
Why that version of mergesort is faster
Python : I am looking for a solid implementation to allow me to progressively work through a list of items using Queue.The idea is that I want to use a set number of workers that willgo through a list of 20+ database intensive tasks and return the result . I want Python to start with the five first items and as soon as...
Picking up items progressivly as soon as a queue is available
Python : I am trying to send a request wtih post method to an API , my code looks like the following one : Actually it works fine but i want to use `` requests '' library instead as it 's more updated and more flexible with proxies with following code : But it returns 403 status code , how can i fix it ? Keep in mind t...
Works with urrlib.request but does n't work with requests
Python : I currently have a list of connections stored in a list where each connection is a directed link that connects two points and no point ever links to more than one point or is linked to by more than one point . For example : Should produce : I have attempt to do this using an algorithm that takes an input point...
How can I order a list of connections
Python : Given the DataFrameI want to create two new columns , picking from either left_* or right_* depending on the content of transformed : And I get the expected resultHowever when I try to do it in one operation I get an unexpected result containing NaN valuesIs there a way to use df.where ( ) on multiple columns ...
Using Pandas df.where on multiple columns produces unexpected NaN values
Python : I 'm trying to build a side navigation bar where categories are listed and upon clicking a category a respective sub list of subcategories is shown right below the category . And if the category is clicked again , the sub list contracts.So I 'm running a loop across category objects . Inside this outer loop , ...
what could cause html and script to behave different across iterations of a for loop ?
Python : Celery - bottom line : I want to get the task name by using the task id ( I do n't have a task object ) Suppose I have this code : And then in some other place : I know I can get the task name if I have a task object , like here : celery : get function name by task id ? .But I do n't have a task object ( perha...
Celery - how to get task name by task id ?
Python : I have pandas dataframe like followingDesired pandas dataframeLike wise I have data for all 24 hours . I do not want to use if else loop for 48 times . Is there any better way of doing this in pandas ? <code> date value 2018-02-12 17:30:00 23 2018-02-12 17:34:00 45 2018-02-12 17:36:00 23 2018-02-12 17:45:00 56...
how to divide pandas date time column in half hourly interval
Python : I have encountered error 'RuntimeError : dictionary changed size during iteration ' while iterating through a dictionary in a thread , which is being inserted in another thread in Python 2.7.I found that by using Global Intrepreter Lock , we could lock a object in mutithreaded situation.I encountered the error...
How could I use GIL for a dictionary in a multithreaded application ?
Python : I stumbled upon a weird and inconsistent behavior for Pandas replace function when using it to swap two values of a column . When using it to swap integers in a column we haveThis yields the result : However , when using the same commands for string valuesWe getCan anyone explain me this difference in behavior...
Pandas weird behavior using .replace ( ) to swap values
Python : In my CSV file I have the following entries : The first column contains date-time in a specific timezone ( GMT+01 ) .I read the CSV file using the following command : As a result I get the following : As we can see timestamp has been modified ( one hour has been added to it ) . My interpretation is that the ti...
How does pandas treat timezone when reading from a CSV file ?
Python : I 'm am using OAuth to allow my user to OAuth with Hunch , on my webpage I have a button to allow the user to go to Hunch and enter their detailsHow can I call a method here rather than a handler ? as it is currently calling this : but when I print the url2 it just prints /hunch ? I hope this makes sense.Also ...
Working with OAuth python
Python : I have a very simple python code : However when I press 'Ctrl-C ' the thread is n't stopped . Can someone explain what I 'm doing wrong . Any help is appreciated . <code> def monitor_keyboard_interrupt ( ) : is_done = False while True : if is_done break try : print ( sys._getframe ( ) .f_code.co_name ) except ...
Why monitoring a keyboard interrupt in python thread does n't work
Python : I have a large number of strings . For my purposes , two strings are equivalent if one is a rotation of the other ( e.g . '1234 ' is equivalent to '3412 ' ) .What is an efficient way to process every string exactly once ( up to rotation ) in Python ? A naive implementation of what I want might look like : <cod...
When strings are equivalent up to rotation
Python : I wrote a simple Sieve of Eratosthenes , which uses a list of ones and turns them into zeros if not prime , like so : I tested the speed it ran with % timeit and got : I assumed , if I changed [ 1 ] and 0 to booleans , it would run faster ... but it does the opposite : Why are the booleans slower ? <code> def ...
Why does my Sieve of Eratosthenes work faster with integers than with booleans ?
Python : My question is different than the following : Question1 : Week of a month pandasQuesiton2 : Week number of the monthThe above question deals with assuming 7 days in a week . It attempts to count the number of 7 days week there are . My data is composed of ( business days ) daily prices and there can be at time...
Pandas Week of the Month Business Day Stock Price Data
Python : With this code , left side gets the first shot at comparison , as documented in the data model : But if we make a slight modification on line 6 ( everything else the same ) : Now the right side gets to have the first shot at comparison . Why is that ? Where is it documented , and what 's the reason for the des...
python equality precedence
Python : I 've been trying to add New Relic to my app but I get this error : The error comes from the Procfile line where I declare the web process . I followed the documentations which states here and here that the proccess should be declared like this : It does n't work.How should I declare the web process in the Pro...
Heroku . New Relic Procfile command does n't work
Python : Given these two dataframes , how do I get the intended output dataframe ? The long way would be to loop through the rows of the dataframe with iloc and then use the map function after converting df2 to a dict to map the x and y to their score.This seems tedious and would take long to run on a large dataframe ....
How to map one dataframe to another ( python pandas ) ?
Python : I have Python Enum class like this : In MYSQL database , seniority ENUM column has values `` Intern '' , `` Junior Engineer '' , `` Medior Engineer '' , `` Senior Engineer '' .The problem is that I get an error : This error has occurred when I call query like : seniority is enum property in the UserProperty mo...
How to define Python Enum properties if MySQL ENUM values have space in their names ?
Python : I have a massive blob of JSON data formatted as follows : I wrote a function that formats it into a pandas dataframe that takes this form : This is the function that writes the JSON to a DataFrame : Unfortunately , with the amount of data I have ( and some simple timers I tested with ) this will take about 4 h...
Speed up JSON to dataframe w/ a lot of data manipulation
Python : Many programming languages , including Python , support an operation like this : which returns the stringI understand that this is the case , but I do n't understand the design decision behind it - surely it would be more semantically valid to perform the join operation on the list , like so : If anyone could ...
Semantics of turning list into string