Datetime error in python: convert int/num to Datetime [duplicate] - python

This question already has answers here:
Convert date from excel in number format to date format python [duplicate]
(2 answers)
Convert Excel style date with pandas
(3 answers)
Closed 1 year ago.
I'd like to convert a column to date. My source data is from an Excel which is already formatted to date data type. However, when pandas read my file the date columns are read as e.g.'44249'
I tried the following code
RPT["Pot"] =`RPT["Pot"].apply(lambda x: pd.to_datetime(x, format='%d%m%Y'))
but I got the error time data '44249' does not match format '%d%m%Y' (match).
I also tried this code:
RPT["PLANNED SUBMISSION DATE TO E&P"] = pd.to_datetime(RPT["PLANNED SUBMISSION DATE TO E&P"])
but the results were 1970-01-01 00:00:00.000044365, which inaccurate.
Can I anyone please help me?

Related

I have a dataframe with a date column in the following format: 20130526T150000. How do you change this column to datetime 2013-05-26 15:00:00? [duplicate]

This question already has answers here:
How to define format when using pandas to_datetime?
(2 answers)
Convert Pandas Column to DateTime
(8 answers)
Closed 5 months ago.
Does anyone know how to change 20130526T150000 to datetime format?
One note: the 'T' is usefull. Use pd.to_datetime() directly, the T is actually usefull as it denotes the iso format and will help not confuse for some locales (some countries have the month first, then the day, others the oposite - iso goes from most significant to less: year, month, day)...
pd.to_datetime("20130526T150000")
Timestamp('2013-05-26 15:00:00')
If you want to be more explicit, specify the format:
pd.to_datetime("20130526T150000", format=...)
However, this might be a duplicate: How to define format when using pandas to_datetime? ... For best results, if you are doing a conversion of a column, use Convert Pandas Column to DateTime

How to convert this into datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I'm currently getting a panda data frame from alpaca - a stock trading platform. The data I'm getting is for minute data and is giving me a timestamp that cant be converted to DateTime to make my functions easier.
Heres an output for AMZN minute data
open high low
timestamp
2019-08-08 14:12:00-04:00 1826.8600 1826.9649 1826.1026
2019-08-08 14:13:00-04:00 1826.6500 1827.6520 1826.6500
Heres the data type:
print(type(AMZN.index[0]))
<class'pandas._libs.tslibs.timestamps.Timestamp'>
I'm trying to split this index into two columns where the day is the index and the time(minutes and hours) is the next column. Or just getting it into all into datetime format together would work fine too.
I've tried finding a similar problem and using _datetime() but it doesn't seem to work. I have the datetime library but it doesn't seem to register when I use the code as in other posts.
Overall Im just trying to find an easy way of iterating over a certain time on any given day and this format is giving me trouble. If someone has a better way of dealing with time series of this type please feel free to share.
Actually Timestamp is one of the easiest data type to manipulate in pandas:
df['Date'] = df.index.date
df['Time'] = df.index.time
df = df.set_index(df['Date])
Result:
open high low Time
Date
2019-08-08 1826.86 1826.9649 1826.1026 14:12:00
2019-08-08 1826.65 1827.6520 1826.6500 14:13:00

Converting an object-type column to a dates in Pandas [duplicate]

This question already has answers here:
Python TypeError: cannot convert the series to <class 'int'> when trying to do math on dataframe
(3 answers)
Closed 4 years ago.
I am working in a project now and the is a column known as 'Date of last recharge' but it's an object-type column but I need to convert it into date formats:
Date of Last Recharge
20-10-2018
23-10-2018
04-08-2018
12-09-2018
20-08-2018
How I went about it was to split each date into it's individual year(y), month(m) and day(d) by using custom functions. But in the process of trying to recombine each of the series into a new column using:
date(y,m,d)
But I end up with this error:
cannot convert the series to (class 'int')
I'm not even sure if this approach is correct, if you know a better way to do this, please let me know.
Try this, it will work ..
df['Date_of_Last_Recharge'] = pd.to_datetime(df['Date_of_Last_Recharge'])

while converting a pd timestamp to date throws an error in python [duplicate]

This question already has answers here:
How do I convert timestamp to datetime.date in pandas dataframe?
(9 answers)
Closed 4 years ago.
I am reading this data from my framework and getting it as below:
mped_date = pd.to_datetime(metadata['MPED'].values[0])
>> mped_date: 2017-12-31 00:00:00
type(mped_date)
>> mped_date: <class 'pandas._libs.tslib.Timestamp'>
I have other variables which are of type date and I am unable to compare this with them.I get the below error
TypeError: Cannot compare type 'Timestamp' with type 'date'
Can someone guide me to convert the mped_date to date type ?
I don't use pandas, but the pandas doc shows a pandas.Timestamp.to_pydatetime method that sounds like it will do the conversion you need: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Timestamp.to_pydatetime.html

Changing Date/Time in python pandas [duplicate]

This question already has answers here:
Convert hh:mm:ss to minutes using python pandas
(3 answers)
Closed 5 years ago.
So I have Date/Time string in a pandas dataframe that looks like this:
2016-10-13 02:33:40
And I need to cut out the year/month/day completely, and convert the time to minutes. So that time/date above needs to be converted into just:
153
^^2 hours and 33 minutes = 153 minutes
I am basically trying to sift the data by the amount of time in between each entry and converting it all to minutes (since the amount of time that passes will not go past a day per session) seemed to make the most sense to me. But, I am open to any other suggestions!
Thanks for the help
lets say the date and time is in a column called DateTime.
df["Datetime"] = pd.to_datetime(df["Datetime"])
df["Minutes"] = 60*df["Datetime"].dt.hour +df["Datetime"].dt.minute
There once the datetime column is in the right format, you can access a lot of properties. See here for more https://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties

Categories