- если столбец «Минимальная дата» равно нулю, то мы берем данные из столбца «Дата начала»,
- в противном случае мы берем дату из «Минимальной даты» в строковом формате, объединяем ее со временем из « Время».
Код: Выделить всё
fffg = pd.DataFrame({'N': [1, 2, 3], 'MinDate': [date(2023,1,2), None, date(2022,1,7)],
'Time': [datetime.time(8, 48, 0), datetime.time(8, 48, 0), datetime.time(8, 48, 0)],
'Start_Date': [datetime.datetime(2022,4,1,15,10), datetime.datetime(2023,4,1,15,10), datetime.datetime(2022,5,1,15,10)]})
fffg['MinDate'] = pd.to_datetime(fffg['MinDate'])
fffg['New'] = np.where(pd.notnull(fffg['MinDate']),
pd.to_datetime(fffg['MinDate'].astype(str)+' '+fffg['Time'].astype(str)),
fffg['Start_Date']
)
fffg
Код: Выделить всё
ValueError: time data "NaT 08:48:00" doesn't match format "%Y-%m-%d %H:%M:%S", at position 1. You might want to try:
- passing `format` if your strings have a consistent format;
- passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
- passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
Подробнее здесь: https://stackoverflow.com/questions/788 ... new-column