xxxxxxxxxx
df[['A','B']] = df[['A','B']].apply(pd.to_datetime) #if conversion required
df['C'] = (df['B'] - df['A']).dt.days
xxxxxxxxxx
t1 = pd.to_datetime('1/1/2015 01:00')
t2 = pd.to_datetime('1/1/2015 03:30')
print pd.Timedelta(t2 - t1).seconds / 3600.0
xxxxxxxxxx
# credit to Stack Overflow user in the source link
import pandas as pd
# df is your pandas dataframe
# if already datetime64 you don't need to use to_datetime
df['A'] = pd.to_datetime(df['A'])
df['B'] = pd.to_datetime(df['B'])
df['diff'] = df['A'] - df['B'] # difference in days
xxxxxxxxxx
interface Example {
percentage: number; // Any number
}
function validateRange(val: number, min: number, max: number): boolean {
return val >= min && val <= max;
}
// Example usage
const data: Example = { percentage: 75 };
if (!validateRange(data.percentage, 0, 100)) {
throw new Error("Percentage must be between 0 and 100");
}
xxxxxxxxxx
type Bounded<T, Brand> = T & { __brand: Brand };
type Range1To100 = Bounded<number, 'Range1To100'>;
interface Example {
score: Range1To100;
}
// Function to enforce range
function createRange1To100(value: number): Range1To100 {
if (value < 1 || value > 100) throw new Error('Value out of range');
return value as Range1To100;
}
const example: Example = { score: createRange1To100(50) }; // OK