Skip to content

ENH: Sampling a contiguous block from a DF #63475

@YM2132

Description

@YM2132

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish I could use Pandas to return a random block of data from a DF.

Like .sample() but the data returned is contiguous, in essence the method randomly samples 1 row idx and then samples the next N from that point.

Feature Description

Add a contiguous param to .sample or create a new .sample_contgiuous

Draw from distribution a single idx

and select N around that as a contiguous block

Alternative Solutions

def random_contiguous_sample(df, n):
max_start = len(df) - n

start_idx = np.random.randint(0, max_start + 1)
return df.iloc[start_idx:start_idx + n]

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions