0

i have applied a function to a column of my dataframe, this column contains the date with year, month, day and hour, minute, second and what i would like to do is to separate year, month, day and put it in a column and hour, minute, second put it in another column at the same time, my code looks like this

def change_format(day):
  if day != 'nan':  
    format_1 = datetime.strptime(day, "%a %b %d %H:%M:%S %z  %Y")

    new_day = format_1.strftime('%d/%m/%Y')
    new_time = format_1.strftime('%H:%M:%S')                               
  return new_day,new_time
concatenar['pubDate']=concatenar['pubDate'].apply(change_format) 

so far in my column it does not return any values

2 Answers2

3

You can convert your tuple output to list using .tolist() and then use pd.DataFrame() to construct the dataframe with the 2 required columns, as follows:

concatenar[["pubDate_date","pubDate_time"]] = pd.DataFrame(concatenar["pubDate"].apply(change_format).tolist(), index=concatenar.index)

Using pd.DataFrame() is much faster than pd.Series(). You can refer to this answer and this answer of some other posts for the speed comparison between using pd.DataFrame() and pd.Series() for building new columns.

SeaBean
  • 22,547
  • 3
  • 13
  • 25
2

Based on your code it's not clear where you want the second column to go. Your current apply function should be returning a column where each entry is a tuple. To unpack it into two columns you have to specify which two columns to put the output and can use apply() again with pd.Series().

concatenar[ ["pubDate_day","pubDate_time"] ] = concatenar["pubDate"].apply(change_format).apply(pd.Series)

You can read more about unpacking columns of tuples here.

Leo
  • 446
  • 2
  • 9