This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed 12 months ago.
I have a pandas dataframe with dates in the following format:
Dec 11, 2018
Wondering is there an easy way to change the format to 11/12/2018? I know I can go through each month manually but not sure what my next step would be to switch around the month and day and add the /.
Thanks in advance!
Use strftime('%m/%d/%Y'):
s = pd.Series(['Dec 11, 2018'])
pd.to_datetime(s).dt.strftime('%m/%d/%Y')
Output:
0 12/11/2018
dtype: object
Related
This question already has an answer here:
Pandas read_excel: parsing Excel datetime field correctly [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to convert an entire column containing a 5 digit date code (EX: 43390, 43599) to a normal date format. This is just to make data analysis easier, it doesn't matter which way it's formatted. In a series, the DATE column looks like this:
1 43390
2 43599
3 43605
4 43329
5 43330
...
264832 43533
264833 43325
264834 43410
264835 43461
264836 43365
I don't understand previous submissions with this question, and when I tried code such as
date_col = df.iloc[:,0]
print((datetime.utcfromtimestamp(0) + timedelta(date_col)).strftime("%Y-%m-%d"))
I get this error
unsupported type for timedelta days component: Series
Thanks, sorry if this is a basic question.
You are calling the dataframe and assigning it to date_col. If you want to get the value of your first row, for example, use date_col = df.iloc[0]. This will return the value.
Timedelta takes an integer value, not Series.
This question already has answers here:
Pandas - convert strings to time without date
(3 answers)
Closed 1 year ago.
I have a column in for stop_time 05:38 (MM:SS) but it is showing up as an object. is there a way to turn this to a time?
I tried using # perf_dfExtended['Stop_Time'] = pd.to_datetime(perf_dfExtended['Stop_Time'], format='%M:%S')
but then it adds a date to the output: 1900-01-01 00:05:38
I guess what you're looking for is pd.to_timedelta (https://pandas.pydata.org/docs/reference/api/pandas.to_timedelta.html). to_datetime operation which will of course always try to create a date.
What you have to remember about though is that pd.to_timedelta could raise ValueError for your column, as it requires hh:mm:ss format. Try to use apply function on your column by adding '00:' by the beginning of arguments of your column (which I think are strings?), and then turn the column to timedelta. Could be something like:
pd.to_timedelta(perf_dfExtended['Stop_Time'].apply(lambda x: f'00:{x}'))
This may work for you:
perf_dfExtended['Stop_Time'] = \
pd.to_datetime(perf_dfExtended['Stop_Time'], format='%M:%S').dt.time
Output (with some additional examples)
0 00:05:38
1 00:10:17
2 00:23:45
This question already has answers here:
Resample a pandas timeseries by "1st Monday of month" etc
(2 answers)
Closed 3 years ago.
I need to generate an array of dates at specific intervals. for example every 3 sunday of the month:
[2019-02-17, 2019-03-17, 2019-04-21]
Is it possible to do this using standard pandas functions? For example, specifying some particular freq field in the pd.date_range method or using pd.Dateoffset?
You could do something like this:
s = pd.date_range('2019-01-01','2019-12-31', freq='D')
s[(s.dayofweek == 6) & (s.day>=15) & (s.day<=21)]
Output:
DatetimeIndex(['2019-01-20', '2019-02-17', '2019-03-17', '2019-04-21',
'2019-05-19', '2019-06-16', '2019-07-21', '2019-08-18',
'2019-09-15', '2019-10-20', '2019-11-17', '2019-12-15'],
dtype='datetime64[ns]', freq=None)
This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
Can someone show me a few lines of code on how to add one day to datetime?
Like if you had some initial date:
start_date = datetime.date(1847, 3, 30)
and simply wanted to change it to (1847, 3, 31)
and then (1847, 4, 1)
and so on.
I'm new to Python and just trying to wrap my head around this import.
startdate + datetime.timedelta(days=1)
will give you the answer
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I read a date in Excel format in Python?
I am reading values from a .XLS(excel) file.While reading a date field,i am getting a float value like 40374.What i need is to convert this float value into date.Please help.
Thanks in advance!!!!
Dates in excel are represented by the number of days since 1/1/1900. So 40374 is 40,374 days after 1/1/1900, or July 15, 2010
Also I believe that if there is anything after the decimal point, this represents a fraction of a day.
so 40374.0 would be 7/15/2010 at midnight, .5 would be noon, .75 is 6pm, etc.