datetime-format

How to swap month and day values in a DataFrame?

How to swap month and day values in a DataFrame? Question: I have a DataFrame in which the first column contains information in the form of a date and time. But for me this information is not recognized correctly. When I took information from the database and sent the data to the DataFrame table, it …

Total answers: 1

Convert DataFrame column from string to datetime for format "January 1, 2001 Monday"

Convert DataFrame column from string to datetime for format "January 1, 2001 Monday" Question: I am trying to convert a dataframe column "date" from string to datetime. I have this format: "January 1, 2001 Monday". I tried to use the following: from dateutil import parser for index,v in df[‘date’].items(): df[‘date’][index] = parser.parse(df[‘date’][index]) But it gives …

Total answers: 2

How to convert float64 time to datetime object

How to convert float64 time to datetime object Question: I am working on Berkeley Earth Surface Temperature data. I have a monthly NetCDF file from 1753-recent. The date axis is float64 and when I convert to DateTime format it only returns the first day and month of each year. Below is the time documentation from …

Total answers: 1

Datetime.strptime format for 2022-08-24T04:57:17.065000+00:00

Datetime.strptime format for 2022-08-24T04:57:17.065000+00:00 Question: We have the following string in a variable: date_variable = "2022-08-24T04:57:17.065000+00:00" We would like to convert this to datetime. The following does not work: timestamp_formatted = datetime.strptime(date_variable, ‘%Y-%m-%d%H:%M:%S.%f+%z’) Please help. Asked By: Jack tileman || Source Answers: You left out the T and %z includes the sign of the time …

Total answers: 1

How Can I Convert datetime.timedelta to int?

How Can I Convert datetime.timedelta to int? Question: I wrote these codes for prediction and test accuracy on some dataset with the help of sklearn random forest classifier. Code a = datetime.datetime.now() y_pred=clf.predict(X_test) b = datetime.datetime.now() rf_time = b-a rf_ac = metrics.accuracy_score(y_test, y_pred) print(type(rf_time)) print(‘Random Forest Time Duration – ‘,b-a) print("Random Forest Accuracy:",metrics.accuracy_score(y_test, y_pred)) Output …

Total answers: 2

Converting datetime format maintaining the datetime data type in python

Converting datetime format maintaining the datetime data type in python Question: I need datetime object in the format mm/dd/yyyy. I tried using strptime: datetime.strptime("05-08-2022","%d-%m-%Y") >>datetime.datetime(2022, 8, 5, 0, 0) That changes the format to YYYY/MM/DD. I know we can change the format using strftime to convert datetime to any format but that results into a …

Total answers: 1

Convert milliseconds to string %H%M%S.%f

Convert milliseconds to string %H%M%S.%f Question: I’m trying to convert time in milliseconds to time in a string with milliseconds. import datetime x = 23500 j = x / 1000.0 print(datetime.datetime.fromtimestamp(j).strftime(‘%H:%M:%S.%f’)) The result is ’03:00:23.500000′. And this is not true. 23500 ms is not 3 hours 23 seconds, but only 23 seconds. The module time …

Total answers: 1

How to solve datetime format error "time data %r does not match format %r"?

How to solve datetime format error "time data %r does not match format %r"? Question: I’m trying to convert a date from string format to a datetime format. I think that I’m using the right format, but the error is still raised. Code example: from datetime import datetime, timedelta format = "%Y-%m-%dT%H:%M:%S.000Z" inctime=’2022-03-21T19:55:23.577Z’ time=datetime.strptime(inctime,formato2) Error: …

Total answers: 2

Count of Year in Python

Count of Year in Python Question: How can I find count of year in python from certain date and a date since people opened an account (CRDACCT_DTE_OPEN)? The certain date (MIS_DATE) is 2021-03-01 with format= ‘%Y%m%d’. The dataset given below: import pandas as pd df = pd.DataFrame( { "MIS_DATE": ["2018-03-02", "2020-03-26", "2019-08-17", "2019-08-17", "2019-08-19"], "CRDACCT_DTE_OPEN": …

Total answers: 3