wrong format for time data pandas - pandas

what is the correct date format for the time
2020-02-24T18:00:00
I have tried using dates_cc = dt.datetime.strptime(dates_c, '%Y-%m-%d%H:%M:%S')
but I get a wrong date format error.

Just adding a T should work:
import datetime as dt
dates_c = "2020-02-24T18:00:00"
dates_cc = dt.datetime.strptime(dates_c, '%Y-%m-%dT%H:%M:%S')
dates_cc
Output:
datetime.datetime(2020, 2, 24, 18, 0)

Related

Add a column of minutes to a datetime in pandas

I have a dataframe with a start time and the length of operation. I'm trying to figure out out to add the length (in minutes) to the start time in order to figure out the end time of the session. I've run a few different variations of the same general idea and keep getting the same error, "unsupported type for timedelta minutes component: Series". The code extract is below:
data= {'Name': ['John', 'Peter'],
'Start' : [2, 2],
'Length': [120, 90],
}
df = pd.DataFrame.from_records(data)
df['Start'] = pd.to_datetime(df['Start'])
df['Length'] = pd.to_datetime(df['Length'])
df["tdiffinmin"] = df['Start'].apply(lambda x: x + pd.DateOffset(minutes = df["Length"]))
Ive also tried the follow as other methods of doing this math and keep getting similar errors.
df["tdiffinmin"] = df['Start'].apply(lambda x: x -pd.DateOffset(minutes = df["Length"]))
df["tdiffinmin"] = (df['Start']. + timedelta(minutes = df["Length"])).dt.total_seconds() / 60
df['tdiffinmin'] = df['Start'] - pd.DateOffset(minutes = df["Length"])
The full code reads from a data set (excel sheet or CSV), populates a Dataframe, and this is some of the math I am doing. Originally it was done with Start and Stop times, so I know something similar is possible. In the dataset, Length is in minutes and Start is a date and time, so datetime is necessary.
You should convert Length into timedelta, not datetime:
df['Start'] = pd.to_datetime(df['Start'])
df['Length'] = pd.to_timedelta(df['Length'], unit='min')
df['tdiffinmin'] = df['Start'] + df['Length']
Output:
Length Name Start tdiffinmin
0 02:00:00 John 1970-01-01 00:00:00.000000002 1970-01-01 02:00:00.000000002
1 01:30:00 Peter 1970-01-01 00:00:00.000000002 1970-01-01 01:30:00.000000002

How to set an index within multiindex for datetime?

I have this df:
open high low close volume
date symbol
2014-02-20 AAPL 69.9986 70.5252 69.4746 69.7569 76529103
MSFT 33.5650 33.8331 33.4087 33.7259 27541038
2014-02-21 AAPL 69.9727 70.2061 68.8967 68.9821 69757247
MSFT 33.8956 34.2619 33.8241 33.9313 38030656
2014-02-24 AAPL 68.7063 69.5954 68.6104 69.2841 72364950
MSFT 33.6723 33.9269 33.5382 33.6723 32143395
which is returned from here:
from datetime import datetime
from iexfinance.stocks import get_historical_data
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd
start = '2014-01-01'
end = datetime.today().utcnow()
symbol = ['AAPL', 'MSFT']
prices = pd.DataFrame()
datasets_test = []
for d in symbol:
data_original = data.DataReader(d, 'iex', start, end)
data_original['symbol'] = d
prices = pd.concat([prices,data_original],axis=0)
prices = prices.set_index(['symbol'], append=True)
prices.sort_index(inplace=True)
when trying to get the day of the week:
A['day_of_week'] = features.index.get_level_values('date').weekday
I get error:
AttributeError: 'Index' object has no attribute 'weekday'
I tried to change the date index to date time with
prices['date'] = pd.to_datetime(prices['date'])
but got this error:
KeyError: 'date'
Any idea how to keep 2 indexs, date + symbol but to change one of them (date) tp datetime so I could get the day of the week?
Looks like the date level of the index contains strings, not datetime objects. One solution is to reset all MultiIndex levels into columns, convert the date column to datetime, and set the MultiIndex back. Then you can proceed with pandas datetime accessors like .weekday in the usual way.
prices = prices.reset_index()
prices['date'] = pd.to_datetime(prices['date'])
prices = prices.set_index(['date', 'symbol'])
prices.index.get_level_values('date').weekday
Int64Index([3, 3, 4, 4, 0, 0, 1, 1, 2, 2,
...
1, 1, 2, 2, 3, 3, 4, 4, 1, 1],
dtype='int64', name='date', length=2516)

Only the string 'symbols' is supported for Nasdaq

import pandas_datareader as web
import datetime
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2018, 7, 30)
f = web.DataReader("AAPL", "nasdaq", start, end)
I run this code and the result is like this
File "/anaconda3/lib/python3.6/site-packages/pandas_datareader/data.py", line 377, in DataReader
"Nasdaq, not %r" % (name,))
ValueError: Only the string 'symbols' is supported for Nasdaq, not 'AAPL'
so how can I fix this one please help.

Get day of year from a string date in pandas dataframe

I want to turn my date string into day of year... I try this code..
import pandas as pd
import datetime
data = pd.DataFrame()
data = pd.read_csv(xFilename, sep=",")
and get this DataFrame
Index Date Tmin Tmax
0 1950-01-02 -16.508 -2.096
1 1950-01-03 -6.769 0.875
2 1950-01-04 -1.795 8.859
3 1950-01-05 1.995 9.487
4 1950-01-06 -17.738 -9.766
I try this...
convert = lambda x: x.DatetimeIndex.dayofyear
data['Date'].map(convert)
with this error:
AttributeError: 'str' object has no attribute 'DatetimeIndex'
I expect to get new date to match 1950-01-02 = 2, 1950-01-03 = 3...
Thank for your help... and sorry Im new on python
I think need pass parameter parse_dates to read_csv and then call Series.dt.dayofyear:
data = pd.read_csv(xFilename, parse_dates=["Date"])
data['dayofyear'] = data['Date'].dt.dayofyear

Struggle with string formatting pandas date

I have the following stringy : 2017-12-03
I am looking forward to turn the str into to_datetime, add a BDay and change the format of such date.
what i tried was :
import datetime as dt
from pandas.tseries.offsets import BDay
valor_nuevo=(pd.to_datetime(stringy,'%Y-%m-%d') + BDay(1)).strftime('%d/%m/%Y')
And outputs as error an AssertionError
You need define parameter format or omit it in to_datetime:
valor_nuevo = (pd.to_datetime(stringy,format='%Y-%m-%d') + BDay(1)).strftime('%d/%m/%Y')
valor_nuevo = (pd.to_datetime(stringy) + BDay(1)).strftime('%d/%m/%Y')
print (valor_nuevo)
04/12/2017