I read a bit into the pandas 2.0.0 change on returning views vs. loc left of the equals sign) and I at least can get the column to being "object" dtype with datetime_df = datetime_df.astype(), but I don't quite understand why especially the first solution works and what I misunderstood about. There are multiple ways to circumvent this that work for me, including simply replacing line 5 withĭatetime_df = datetime_df.loc.dt.strftime("%d.%m.%Y") (omitting the. Print(datetime_df.dtypes) # will return "datetime64" for this single column ![]() Not_yet_datetime_df = pd.DataFrame(]).Tĭatetime_df = not_yet_datetime_df.astype("datetime64")ĭatetime_df.loc = datetime_df.loc.dt.strftime("%d.%m.%Y")ĭatetime_df.loc = datetime_df.loc.astype("object") # neither of these two will work for me The code to reproduce this (occurs in both pandas 2.0.0 and 2.0.1, this might cause the problem) is as follows (in the actual DataFrame I am using there are many more than a single column): import pandas as pd I have found ways to circumvent the problem and get pandas to do what I need, but either this is a bug or I do not understand some underlying mechanic which could come back to bite me in the longer run, hence my question. loc to get the column to be changed and assign them with the same column converted to strings. At some point in my code I need to convert one column from datetime to string for writing back to Excel, but pandas will not let me do that if I try it the way that would be most obvious to me and seems to be the recommended way of doing it according to documentation: using. The columns containing strings are of dtype "object" while the date-columns are of dtype "datetime64". The strptime() method creates a datetime object from a string.I'm using pandas to load data from Excel with the resulting DataFrame containing both strings and dates. We also recommend you to check Python strptime(). When you run the program, the output will be:įormat codes %c, %x and %X are used for locale's appropriate date and time representation. Locale’s appropriate time representation.Įxample 3: Locale's appropriate date and time Locale’s appropriate date representation. Locale’s appropriate date and time representation. All days in a new year preceding the first Monday are considered to be in week 0. Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. Week number of the year (Sunday as the first day of the week). Microsecond as a decimal number, zero-padded on the left.ĭay of the year as a zero-padded decimal number. Hour (12-hour clock) as a decimal number. Hour (12-hour clock) as a zero-padded decimal number. Hour (24-hour clock) as a decimal number. ![]() Hour (24-hour clock) as a zero-padded decimal number. Year without century as a decimal number. Year without century as a zero-padded decimal number. Directiveĭay of the month as a zero-padded decimal. The table below shows all the codes that you can pass to the strftime() method. When you run the program, the output will be: Date time object: 09:55:22
0 Comments
Leave a Reply. |