


Create random datetime column conditional on another datetime column pandas
I have a pandas data frame df_sample:
columna columnb a aa a ab b ba b bb b bc
I have created a random column containing some date objects:
df_sample['contract_starts'] = np.random.choice(pd.date_range('2024-01-01', '2024-05-01'), len(df_sample))
This results in the following output:
columna columnb contract_starts a aa 2024-01-21 a ab 2024-03-03 b ba 2024-01-18 b bb 2024-02-18 b bc 2024-04-03
How to create another datetime column contract_noted that also has a given range of values (e.g. until 2024-05-01) but does not exceed contract_starts
column, for example:
columnA columnB contract_starts contract_noted A AA 2024-01-21 2024-01-20 A AB 2024-03-03 2024-01-01 B BA 2024-01-18 2024-01-13 B BB 2024-02-18 2024-02-01 B BC 2024-04-03 2024-03-28
Correct answer
You can subtract a random time increment from the contract_starts column by
numpy.random. randint andto_timedelta
:< /a>
df_sample['contract_noted'] = (df_sample['contract_starts'] -
pd.to_timedelta(np.random.randint(1,30, len(df_sample)),
unit='d'))
print (df_sample)
columna columnb contract_starts contract_noted
0 a aa 2024-04-18 2024-03-21
1 a ab 2024-02-12 2024-01-22
2 b ba 2024-02-21 2024-02-02
3 b bb 2024-04-12 2024-03-29
4 b bc 2024-02-10 2024-02-03
contract_starts generate inetegers between
1 and the difference from the start date and time:
days =(df_sample['contract_starts'] - pd.Timestamp('2024-01-01')).dt.days print (days) df_sample['contract_noted'] = (df_sample['contract_starts'] - pd.to_timedelta(np.random.randint(1,days, len(df_sample)), unit='d')) print (df_sample) columnA columnB contract_starts contract_noted 0 A AA 2024-02-09 2024-01-09 1 A AB 2024-04-26 2024-02-23 2 B BA 2024-04-10 2024-04-06 3 B BB 2024-01-31 2024-01-07 4 B BC 2024-01-14 2024-01-08
The above is the detailed content of Create random datetime column conditional on another datetime column pandas. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
