#πŸ”’ Optimal spot to create DataFrames

9 messages Β· Page 1 of 1 (latest)

weak radish
#
coh_df = pd.DataFrame(coh_matrix["cut_coh_table"], columns=None, index=None)
chan_df = pd.DataFrame(coh_matrix["chanmatrix"], columns=None, index=None)

def _get_unique(comb_index : int):
    ''' Finds and returns an array of unique channels and
    the number of times those channels were counted.
    
    Parameters
    ----------
    comb_index : int
        Index number for which comb is being used
    
    Returns
    -------
    curr_chan_uq : 1darray
        A 1darray of the unique channels that captured the
        frequencies of the current comb
    
    curr_chan_count : 1darray
        A 1darray filled with the counts of each corresponding
        channel in curr_chan_uq
    '''
    # Gets the bin indexes for a single comb set based off comb_index
    bin_index = slt.match_bins(coh_matrix["frequencies"], freqs[combs_dict[combs_list[comb_index]]])
    
    # Splits coh_df and chan_df index-wise by bin_index
    coh_arr = np.array(coh_df.loc[sorted(bin_index)])
    chan_arr = np.array(chan_df.loc[sorted(bin_index)])

    # Returns an array containing only channels where
    # their corresponding coherence value is over 0.05
    chan_arr_sort = chan_arr[coh_arr > 0.05]

    # Returns two arrays containing the unique channels found as well as
    # their counts
    curr_chan_uq, curr_chan_count = np.unique(chan_arr_sort, return_counts=True)
    
    return curr_chan_uq, curr_chan_count
... # bunch of other code that never uses coh_df or chan_df
del coh_df, chan_df

The above code is a function (used to get unique data points) within a module. Right now I'm creating the dfs needed for this before the function is defined, but never use them outside of said function. I'm wondering if it would be better to just create and delete the dfs within the function (and therefore every time it is run), or if I should keep it as is.

cursive arrowBOT
#

@weak radish

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

latent hollow
#

Sounds like it. If it's easy to pass in whatever the function needs, the function should probably make the data frames.

#

Inside the function you won't need to del the dfs, because they will del automatically when you leave the function.

#

@weak radish ^^

weak radish
#

Alrighty, thanks

#

!close

cursive arrowBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.