Python - convert string to simple YYYY date format [duplicate] - python

This question already has answers here:
Convert date to months and year
(4 answers)
Closed 4 years ago.
New to Python, and have already spent 30 minutes reviewing old responses, but still can't figure this out.
'Year' variable is a string. Examples: 1990, 2010.
I need to convert to date format, but just with the 4 year "digits".
Tried the following, but none are working:
date1 = datetime.datetime.date('Year', "%Y")
datetime.datetime.strftime('Year', "%Y")
wcData.astype(str).apply(lambda x: pd.to_datetime('Year', format='%Y'))
df.astype(str).apply(lambda x: pd.to_datetime(x, format='%Y%m%d'))
Please help!

You need datetime.datetime.strptime, not strftime:
import datetime
datetime.datetime.strptime("2010", "%Y")
Here's how to remember which one to use:
strPtime means "string -> parse -> time", that is, parse a string to create some kind of object that represents time;
strFtime means "string <- format <- time" (note the reversed arrows), that is, given an object that represents time, create a string representation of it using some format.

Related

How to convert double values from df to year values/strings? [duplicate]

This question already has answers here:
Convert floats to ints in Pandas?
(11 answers)
Closed last year.
I have a dataframe with year values like: 2014.0, 2013.0... as float values. I would need to convert these values to year format, like 2014, 2013... I tried converting them to string, but I get "2014.0", "2013.0"...
How could I convert them to year, not double or strings?
This is what I tried for string conversion:
df['year']=df['year'].astype(str)
And then:
df['year']= pd.to_datetime(df['year'], format='%Y')
But this gave me results like: 1640995200000 and so on. How could I convert the doubles to reasonable year format?
simply do this !!works!!
df['year']=df['year'].astype(int)
output:
2004
2005
2017
converting to str does this 2014.0 => "2014.0" so that wont help you and converting using datetime will add whole date. so if you need only that particular year do this int remains datatype of that column not datetime objects

time data '09-01-2014 00:01' does not match format '%m/%d/%Y %H:%M:%S' (match) [duplicate]

This question already has an answer here:
Converting to datetime, assuming a part of the date can miss
(1 answer)
Closed 1 year ago.
I don't know why I am getting this again and again.
here's is my code snippet.
my data have the date in MDY format, and time in H: M: S format.
The problem ist that you specify a format while your data contains two different formats: One with seconds (the last five values) and one without (the first five values). Your specified format doesn't fit both.
However, pandas can figure it out by itself if you don't specify the format:
work_data['Date/Time'] = pd.to_datetime(work_data['Date/Time'])

How to convert Python DataFrame column with Excel 5 digit date to yyyy-mm-dd [duplicate]

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.

Converting Pandas Object to minutes and seconds [duplicate]

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

Convert complex time string to datetime [duplicate]

This question already has answers here:
In Pandas, how to convert a string to a datetime object with milliseconds?
(2 answers)
Closed 2 years ago.
Attempting the following pd.to_datetime('2020-08-29 18:00:09:438') results in an error ValueError: ('Unknown string format:', '2020-08-29 18:00:09:438') .
How would I convert this string to a datetime or timestamp?
Let us pass the correct format
pd.to_datetime('2020-08-29 18:00:09:438', format = '%Y-%m-%d %H:%M:%S:%f')
Out[71]: Timestamp('2020-08-29 18:00:09.438000')

Categories

Resources